Python 将数字移到字符串的末尾
在本文中,我们将学习一个Python程序,将数字移动到字符串的末尾。
使用的方法
以下是完成此任务的各种方法:
- 使用isdigit()和for循环
-
使用isdigit()和join()函数
-
不使用任何内置函数
-
使用isnumeric()函数
-
使用isalpha()函数
示例
假设我们已经输入了一个字符串。现在,我们将使用上述方法将输入字符串中包含的所有数字移动到字符串的末尾。
输入
inputString = 'hello5691 tutorialspoint1342'
输出
Resultant string after adding digits at the end:
hello tutorialspoint56911342
以下是所有数字 56911342 包含在输入字符串中的位置被移到字符串的末尾。
方法1:使用isdigit()和for循环
步骤
以下是执行所需任务的算法/步骤-
- 创建一个变量来存储输入字符串。
-
创建一个空字符串以存储结果字符串。
-
创建另一个空字符串以存储所有数字。
-
使用 for循环 遍历输入字符串的每个字符。
-
使用 isdigit() 函数对当前字符应用,使用 if条件语句 检查当前字符是否为数字。
-
如果条件为真,则将当前字符添加到数字字符串中。
-
否则将该字符添加到上述结果字符串中。
-
使用+运算符(字符串连接)将数字添加到结果字符串的末尾。
-
在将数字添加到字符串末尾后,打印结果字符串。
示例
下面的程序使用for循环和isdigit()函数在输入字符串末尾添加所有数字后返回一个字符串 –
# input string
inputString = 'hello5691 tutorialspoint1342'
# printing the input string
print("Input string: ", inputString)
# storing resultant string
resultantString = ''
# storing all digits
digits = ''
# traversing through each character of the input string
for c in inputString:
# checking whether the current character is a digit or not
if c.isdigit():
# add that character to a digits string, if the condition is true
digits += c
else:
# else add that character to the above resultant string
resultantString += c
#concatenating/adding digits at the end of the resultant string
resultantString += digits
# printing resultant string after adding digits at the end
print("Resultant string after adding digits at the end:\n", resultantString)
输出
执行程序后,上述程序将生成以下输出:
Input string: hello5691 tutorialspoint1342
Resultant string after adding digits at the end:
hello tutorialspoint56911342
方法2:使用isdigit()和join()函数
join()函数 − join()是Python中的一个字符串函数,它用于将序列中的元素连接在一起,元素之间由一个字符串分隔。这个函数连接序列元素并转换为字符串。
示例
以下程序使用isdigit()和join()函数,在输入字符串的末尾添加所有数字后返回一个字符串-
# input string
inputString = 'hello5691 tutorialspoint1342'
# printing the input string
print("Input string: ", inputString)
#traversing through each character of a string and getting all digits
digits = ''.join(c for c in inputString if c.isdigit())
# getting all other characters(not digits)
resultantString = ''.join(c for c in inputString if not c.isdigit())
# concatenating/adding digits at the end of the resultant string
resultantString += digits
print("Resultant string after adding digits at the end:\n", resultantString)
输出
Input string: hello5691 tutorialspoint1342
Resultant string after adding digits at the end:
hello tutorialspoint56911342
方法3:不使用任何内置函数。
示例
以下程序返回一个字符串,在输入字符串的末尾添加所有数字,不使用任何内置函数 –
# input string
inputString = 'hello5691 tutorialspoint1342'
# printing the input string
print("Input string: ", inputString)
# storing all digits
digits_str = "0123456789"
# storing all characters except digits
resultantString = ''
# storing all digits of an input string
resultDigits = ''
# traversing through each character of a string
for c in inputString:
# checking whether that current character is in the above digits string
if c in digits_str:
# adding that character to the resultDigits string
resultDigits += c
else:
# else adding that character to the resultant string
resultantString += c
# concatenating/adding digits at the end of the resultant string
resultantString += resultDigits
print("Resultant string after adding digits at the end:\n", resultantString)
输出
Input string: hello5691 tutorialspoint1342
Resultant string after adding digits at the end:
hello tutorialspoint56911342
方法4:使用isnumeric()函数
isnumeric()函数 - 如果字符串中的所有字符都是数字(0-9),则返回 True ,否则返回 False 。
示例
以下程序使用isnumeric()函数将所有的数字添加到输入字符串的末尾,并返回结果字符串。
# input string
inputString = 'hello5691 tutorialspoint1342'
# printing the input string
print("Input string: ", inputString)
# storing all characters except digits
resultantString = ''
# storing all digits
digits = ''
# traversing through each character of an input string
for c in inputString:
# checking whether the current character is a digit
if c.isnumeric():
# adding that digit to the above digits string
digits += c
else:
# else adding that character to the resultantString
resultantString += c
# concatenating/adding digits at the end of the resultant string
resultantString += digits
print("Resultant string after adding digits at the end:\n", resultantString)
输出
Input string: hello5691 tutorialspoint1342
Resultant string after adding digits at the end:
hello tutorialspoint56911342
方法5:使用isalpha()函数
isalpha()函数 - 如果所有字符都是字母(a-z),则返回 True ,否则返回 False 。
示例
以下程序使用isalpha()函数将所有数字添加到输入字符串的末尾,然后返回字符串。
# input string
inputString = 'hello5691 tutorialspoint1342'
# printing the input string
print("Input string: ", inputString)
# storing all characters except digits
resultantString = ''
# storing all digits
digits = ''
# traversing through each character of a string
for c in inputString:
if(c!=' '):
# checking whether current character is an alphabet
if c.isalpha():
# adding that character to the resultantString
resultantString += c
else:
# else adding that digit to the digits
digits += c
# concatenating/adding digits at the end of the resultant string
resultantString += digits
print("Resultant string after adding digits at the end:\n", resultantString)
输出
Input string: hello5691 tutorialspoint1342
Resultant string after adding digits at the end:
hello tutorialspoint56911342
结论
在本文中,我们介绍了5种将数字移动到字符串末尾的不同方法。我们还实现了同样的程序,但没有使用内置函数。