关于#字符串#的问题

oh no!
又是Python让人崩溃的一天

问题1:设定字符串“str="98 76 5 21 Hello Python 23 56 8"”,去掉test_str中两侧的数字和空格后输出.

问题2:假设有一段英文“He is a student,he is a boy,and he is 18 years. he is not a woke.”,其中有单独的字母“a”误写为“A”,请编写程序进行改正

1. 
方法一:两侧数字

import string
print(str1.strip().strip('1234567890'))

方法二:去掉所有数字
str2 = 'abcd 1234 efg 567 asdf '
newstring = ''.join([i for i in str2 if not i.isdigit()])
print(newstring.strip())

2.
方法一:自动大写
str3 = 'He is a student,he is A boy,and he is 18 years. he is not A woke.'
print(str3.title()) 
方法二: replace
str3 = str3.replace(" A ", " a ")   //注意空格

不需要崩溃,给个例子参考一下吧

ss="98 76 5 21 Hello Python 23 56 8"
arr = [s for s in ss.split(' ') if not s.isdigit()]
for a in arr:
    print(a, end=' ')
print()

en_str = 'He is a student,he is A boy,and he is 18 years. he is not A woke.'
en_str = en_str.replace(" A ", " a ")
print(en_str)

img

如有帮助,请采纳

  1. str = "98 76 5 21 Hello Python 23 56 8"
    while str[0].isdigit():
     str = str.strip(string.digits).strip()
    
    print(str)
1. s = str.strip('0123456789 ')
    print(s)
2. s = str.replace(" A "," a ")
    print(s)