Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python从字符串中获取数字_Python_Python 3.x - Fatal编程技术网

如何使用python从字符串中获取数字

如何使用python从字符串中获取数字,python,python-3.x,Python,Python 3.x,我想从字符串中获取数字 例如: a = '22,123 games' 结果: result = 22123 首先你需要找到号码在哪里。 我假设它在第一个空格之前,所以 number = a[0 : a.find(' ')] 然后必须删除逗号: noCommas = number.replace(',', '') 然后转换为int: res = int(noCommas) 你的密码在哪里?

我想从字符串中获取数字

例如:

a = '22,123 games'
结果:

result = 22123

首先你需要找到号码在哪里。 我假设它在第一个空格之前,所以

number = a[0 : a.find(' ')]
然后必须删除逗号:

noCommas = number.replace(',', '')
然后转换为int:

res = int(noCommas)

你的密码在哪里?