Python Learning-string practice

Source: Internet
Author: User

Python string Practice
    1. 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
    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,

    1. (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:

    1. (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.

    • Input Description:
      Input is a string consisting of uppercase letters with a length of less than 100

    • Output Description:
      If small easy to like output "likes", do not like output "dislikes"

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.