Exercise: Eliminate numbers:
The requirements are as follows:
1. Write a program code. After the program is run,
the user is required to input a string containing numbers and letters at will;
2. The program will automatically delete the numbers in the string and
then output a string without numbers ( A string of pure letters) or a list (without numbers);
3. The order of the non-number characters required to be output cannot be changed.
method one:
def delNumber1(str1):
for i in str1:
#if i=='0' or i=='1' or i=='2' or i=='3' or i=='4' or i=='5' or i=='6' or i=='7' or i=='8' or i=='9':
if i in ['0','1','2','3','4','5','6','7','8','9']:
continue
print(i,end="")
print()
Method Two:
知识拓展:在python中,默认是按照ascii的大小比较的;
Strings are compared bit by bit. Whichever of the ASCII codes of the first character of the two strings is larger
will have a larger string, and the subsequent ones will not be compared; if
the first character is the same, it will be larger than the second string, and so on.
Note: The ASCII code of a space is 32, the ASCII code of an empty (null) is 0, and the
ASCII codes of uppercase letters and lowercase letters are different.
def delNumber2(str1):
a=[]
for i in range(len(str1)):
if str1[i]<'0' or str1[i]>'9':
a.append(str1[i])
str1=''.join(a)
print(str1)
Method three:
知识拓展: ord()可以将字符转换为ASCII码 chr()可以将ASCII码转换为字符:
def delNumber3(str1):
for i in str1:
if ord(i)>=48 and ord(i)<=57:
continue
print(i,end="")
print()
Call functions
str1=input('请输入一段包含有数字和字母的字符串:')
delNumber1(str1)
delNumber2(str1)
delNumber3(str1)
operation result
If you feel that you have gained something, you are welcome to give me a reward———— to encourage me to output more high-quality content