Python learning 2, common string operations, python learning strings

Source: Internet
Author: User

Python learning 2, common string operations, python learning strings

Strings are the most widely used in daily development. Now we can summarize some common operations on strings in python.

First, declare a string variable.

1 str = "hello world"

Next we will introduce each method in sequence.

Print (str. capitalize () # uppercase letters of the string
Print (str. casefold () # lowercase letters of all strings
Print (str. center (30, '-') # Place the string in the center of the second parameter. The first numeric parameter must be the length of the modified string # output --------- hello world ----------
Print (str. count ("l",) # count the number of occurrences of a character in the string. The second parameter is the start position, and the third parameter is the end position. At this time, the printed data is 2.
 
Str2 = "Hello World"
Print (str2.encode ("GBK") # encode the string in the specified format. The default value is UTF-8.
Print (str. endswith ("d") # judge whether it is ended with a parameter character
Print (str. find ("l",) # query the index value that appears for the first time within the index range. The print value is 2.
Print (str. index ("l",) # query the index value that appears for the first time within the index range. The print value is 2,

Let's talk about the difference between index () and find (). If index cannot find the index of this character within the range, an error is returned, while find will return-1 without an error.

 

Print (str. isalnum () # Check whether the string is composed of letters and numbers. print false. Because the string contains spaces, spaces are also detected in print (str. isalpha () # checks whether a string is composed of only letters and prints false. Because the string contains spaces, spaces are also detected in print (str. isdecimal () # Check whether the string contains only the decimal character print (str. isdigit () # checks whether a string is composed of only numbers (str. islower () # Check whether the string is composed of lowercase letters print (str. isnumeric () # checks whether a string is composed of only digits. I do not know the difference between it and sidigit. isspace () # Check whether the string is composed of only white spaces print (str. istitle () # checks whether all characters in a string are spelled in uppercase and other characters are printed (str. isupper () # checks whether all the letters in the string are uppercase.

 

# The join method is to link a string sequence, such as a list or ancestor, to str3 = ["h", "e", "l", "l ", "o"]; str4 = ("w", "o", "r", "l", "d") print ("-". join (str3) print ("*". join (str4) # print the following h-e-l-ow * o * r * l * d
Print (str. ljust (20, "_") # returns the left alignment of an original string and fills it with spaces to the new string of the specified length. If the specified length is less than the length of the original string, # output hello world _________
Print (str. lstrip ("h") # used to intercept spaces or specified characters on the left of the string # output ello world
Print (str. replace ("l", "a") # replace the characters in the string. The first parameter is the old character, and the second parameter is the new fee to be replaced, the third parameter is the maximum number of replacement times. You can leave it empty # output heaao woradprint (str. replace ("l", "a", 1) # If the third parameter is added, for example, replace only once # output healo world
Print (str. split ("") # Slice: splits the string into a list according to the characters in the parameter. The second parameter can specify the number of slice times # output ['hello ', 'World'] print (str. rsplit ("l", 1) # Slice. Different from split, the slice starts from the throne. # output ['Hello wor', 'D']
Print (str. startswith ("h") # Check whether print (str. strip () # Remove the specified character from the string header and tail. The default value is space print (str. swapcase () # convert string uppercase and lowercase print (str. title () # uppercase letters at the beginning of each word in the string and print (str. upper () # convert lowercase letters to uppercase print (str. zfill () # returns a string of the specified length. The original string is right-aligned and filled with 0 in front.

 

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.