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种将数字移动到字符串末尾的不同方法。我们还通过不使用内置函数实现了相同的程序。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP