Python string Practice
- Enter a line of characters, counting how many words there are, separated by a space between each of the two words. As input: This is a C + + program. Output: There is 5 words in the line. "Assessment Knowledge Point: String Manipulation"
Code:s=input("请输入一行句子:")list = s.split(‘ ‘)print("There are %d words in the line." %len(list))
Operation Result:
In addition, considering that sometimes the hand shakes a lot of space, and then think of a method:
count = 0s=input("输入字符:")for i in range(len(s)): if i+1 > len(s); count+=1 else: if s[i] == ‘ ‘ and s[i+1] != ‘ ‘: count+=1
- Give a string, in the program to assign the initial value to a sentence, such as "he threw three free throws", the self-coding function to complete the following functions:
1) Find out the number of characters in the character list (for example, the output is 26);
2) Calculate the frequency of the occurrences of each character in a sentence (stored through a dictionary); ---Learn the dictionary and realize it
3) Storing the statistical information in the file "statistics. txt"; ---Finish the file operation and then implement
Code:def function(s):print("字符串中字符的个数为: %d" %len(s))dict = {}for i in s: if i in dict: dict[i] += 1 else: dict[i] = 1f = open("统计.txt","w")for i in dict: f.write(i+":"+str(dict[i])+"\t")f.close()string = input("请输入字符串:")function(string)
Execution Result:
You can see that the "statistics. txt" file is generated. Open to see if the content is written correctly,
- (2017-Good future-written test programming questions)--practice
Title Description:
Enter two strings, removing all the characters from the second string from the first string. For example, enter "they is students." and "Aeiou", the first string after deletion becomes "Thy R stdnts."
Input Description:
Each test input consists of 2 strings
Output Description:
Output the deleted string
- Example 1:
输入 They are students. aeiou输出 Thy r stdnts.
Code:
str1 = input("请输入第一个字符串:")str2 = input("请输入第二个字符串:")str3 = ‘‘for i in str2: if i not in str3: str3+=ifor i in str3: str1=str1.replace(i,‘‘)print(str1)
Operation Result:
- (2017-NetEase-written test programming questions)-string practice
Small, easy-to-like words have the following characteristics:
1. Words each letter is uppercase
2. Words have no consecutive equal letters
The column may not be contiguous.
For example:
Xiao Yi does not like "ABBA", because there are two consecutive ' B '
Small easy to Like "A", "ABA" and "ABCBA" these words
Give you a word, you have to answer whether Xiao Yi will like the word.
Example 1:
输入 AAA输出 Dislikes
Code:
s = input("请输入字符串:")for i in range(len(s)): if s[i] < ‘A‘ or s[i] >‘Z‘: print("Dislike") break else: if i < len(s)-1 and s[i] == s[i+1]: print("Dislike") breakelse: print("Likes")
Execution Result:
Python Learning-string practice