In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you what the common manipulation of strings in Python is, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Today's Python practice time, let's take a look at three exercises related to string manipulation. Mainly practice the conversion and splicing of several common string functions and string numbers.
Question 1
Write a program to complete the following functions: enter a mixed letter and number sentence, determine how many letters, how many numbers, and output.
Give an example
Input: hello world! 123
Output:
LETTERS 10
DIGITS 3
Topic analysis
To judge which are numbers and which are letters in a sentence, you only need to use the function that comes with the string. Then judge through a loop, and save the corresponding type of numbers in a dictionary.
Reference s = input ("input:")
D = {"DIGITS": 0, "LETTERS": 0} # defaults to 0
For c in s:
If c.isdigit ():
D ["DIGITS"] + = 1
Elif c.isalpha ():
D ["LETTERS"] + = 1
Print ("LETTERS", d ["LETTERS"])
Print ("DIGITS", d ["DIGITS"])
Question 2
Write a program to complete the following functions: enter a mixed letter and number sentence, determine how many letters, how many numbers, and output.
Give an example
Enter: hello World!
Output:
UPPER CASE 1
LOWER CASE 9
Topic analysis
This topic is similar to the previous one. Knowledge no longer judges numbers or letters, but here determines whether they are uppercase or lowercase. Considering the expansibility of the program, the answer framework of the previous question does not need to be changed, only need to change the judgment function, and the corresponding input and output can be slightly modified.
Reference: s = input ("input:")
D = {"UPPER CASE": 0, "LOWER CASE": 0}
For c in s:
If c.isupper ():
D ["UPPER CASE"] + = 1
Elif c.islower ():
D ["LOWER CASE"] + = 1
Print ("UPPER CASE", d ["UPPER CASE"])
Print ("LOWER CASE", d ["LOWER CASE"])
Question 3
The result of a calculation spelled out according to a certain rule based on an input number. The rule here is to enter a, and the result format is: a+aa+aaa+aaaa
For example:
Enter 9
Output: 11106
Topic analysis
Here is a mixed topic of numbers and strings, which requires string splicing and string-to-number conversion. The following is stitched with the traditional c language form% s replacement and the newer f-string in python.
Reference answer 1a = input ("enter a number:")
N1 = int ('s'a)
N2 = int ("% s% s"% (a))
N3 = int ("% s%s%s"% (amam a))
N4 = int ("% s%s%s%s"%)
Print (a, n1+n2+n3+n4)
Print (int (f'{a}') + int (f'{a} {a} {a}'))
Reference answer 2a = input ("enter a number:")
Print (int (f'{a}') + int (f'{a} {a} {a}'))
The above is what the common string operations in Python are, and have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.