[Start from scratch] Python string operation method, python string

Source: Internet
Author: User

[Start from scratch] Python string operation method, python string

1. String Length

# Strlen (str) # String Length function name
Str = 'append' # assign the string "apples" to the variable str
Print (len (str) # print the length of the string

 

2. Search for characters

# Strchr (str1, str2) # Find the character function name
Str1 = 'append'
Str2 = 'E'
Result = str1.index (str2) # search for str2 in str1
Print (result)

 

3. Copy the string

# Strcpy (str1, str2) # copy the string function name
Str1 = 'append'
Str2 = str1 # assign str1 to str2
Str1 = 'banas' # assign a new value to str1
Print (str2)

 

4. connection string

# Strcat (str1, str2) # Name of the function connecting strings
Str1 = 'append'
Str2 = 'banas'
Str1 + = str2 # concatenate string str1 and string str2 and assign the value to str1.
Print (str1)

 

5. case-sensitive conversion in strings

Str. lower () # lower case
Str. upper () # uppercase
Str. swapcase () # case-insensitive swaps
Str. capitalize () # uppercase letters

# Strupr (str)
Str = 'abcdefgh'
Str = str. upper ()
Print (str)

# Strlwr (str)
Str = 'abcdefgh'
Str = str. lower ()
Print (str)

 

6. Reverse the string

# Strrev (str) # reverse the string function name
Str = 'abcdefg'
Str = str [:-1] # index from start to end. The third parameter-1 indicates reverse order.
Print (str)

 

7. Search for strings

# Strstr (str1, str2) # Find the string function name
Str1 = 'abcdefg'
Str2 = 'cde'
Print (str1.find (str2) # search for str2 in str1

# Variable. find ("content to be searched", [start position, end position]), start position and end position, indicating the range to be searched. If it is null, it indicates searching for all. After searching, the system returns the position starting from 0. If the position is not found, the system returns-1.

 

8. String replacement

Str = 'abcabcabc'
Str = str. replace ('A', '0') # replace the character "a" in the string 'str' with "0" and assign a new value to str.
Print (str)

 

9. String alignment

Str. ljust (n, [fillchar]) # output n characters. str is left aligned. Fill the remaining parts with fillchar. The default value is space.
Str. Exactly ust (n, [fillchar]) # Same as above, right alignment
Str. center (n, [fillchar]) # Same as above, center

 

10. Other functions

Str. count (char, [start, [end]) # calculate the number of times a character char appears in the string str. You can set where it starts and ends.

Str. strip ([chars]) # Remove all characters before and after chars in str, which can be understood as replacing chars before and after S with None
Str. lstrip ([chars]) # Remove the left part of the character chars
Str. rstrip ([chars]) # Remove the right part of the character chars

Str. split ([sep, [maxsplit]) # Use sep as the separator to split str into a list. maxsplit indicates the number of splits. The default Delimiter is a blank character.

Str. startswith (char) # whether to start with char
Str. endswith (char) # End with char
Str. isalnum () # Whether str is full of letters and numbers
Str. isalpha () # Whether str is all letters
Str. isdigit () # is str all numbers?
Str. isspace () # Whether str is all blank characters
Str. islower () # Are all lowercase letters in str
Str. isupper () # indicates whether the letters in str are uppercase letters.
Str. istitle () # Whether str is capitalized

 

11. String Slicing

Str = 'abcdefghigklm ′
Str [0: 3] # truncates the first to third characters # "abc"
Str [:] # truncates all characters in a string # "abcdefghigklm"
Str [6:] # truncates the seventh character to the end # "higklm"
Str [:-3] # capture the first to last three characters # "abcdefghig"
Str [2] # truncate the third character # "c"
Str [-1] # truncates the first to last character # "m"
Str [:-1] # reverse string # "mlkgihgfedcba"
Str [-3:-1] # truncates the characters before the last and last digits # "kl"
Str [-3:] # truncate the last third digit to the end # "klm"
Str [:-5:-3] # truncate the last four characters in reverse order, and intercept the last two characters # "mg"

Related Article

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.