我正试图打印出字符串中的数字。由于某种原因,输出有点不正常
def get_numerals(string):
for i in string:
if i in "1234567890":
print(i,end="")
else:
print("",end = "")
return i
print(get_numerals("CS1301"))
print(get_numerals("Georgia Institute of Technology"))
print(get_numerals("8675309"))
输出:
13011
y
86753099
无论是数字还是字符,都返回最后一个字符。您的版本的一个简单解决方法是:
或者您可以使用正则表达式:
基本方法是:
输出:
使用
Regular expression
还可以从字符串中获取数字输出:
相关问题 更多 >
编程相关推荐