Python Level 2 Weekly Exercises 19

Exercise 1:

s='japan,china,england'
1. Capitalize the first letter of all words in the string and display the result
2. Replace china with spain and display the result
3. Determine whether all words in the string have the first letter size, and display the result.
4. Capitalize all words in the string and display the result.
Tip
Insert image description here

Answer:

s='japan,china,england'
print(s.title())
print(s.replace('china','spain'))
print(s.istitle())
print(s.upper())

Exercise 2:

Write a program that executes repeatedly and asks the user to enter a string.
If the length of the input string is an odd number, the middle character of the string is output.
If the length of the string is an even number, the last character of the string is output.
Then repeat the above operation

Answer:

while True:
    n=input('请输入一个字符串:')
    if len(n)%2==0:
        print(n[-1])
    else:
        print(n[((len(n)-1)//2)])

If you feel that you have gained something, you are welcome to give me a reward———— to encourage me to output more high-quality contentInsert image description here

Guess you like

Origin blog.csdn.net/weixin_40762926/article/details/132952795