How to manipulate Python strings

Source: Internet
Author: User
1. String length

#strlen (str) # string length function name
Str= ' Apples ' # assigns the string "apples" to the variable str
Print (len (str)) # Prints the length of the string

2. Find characters

#strchr (STR1,STR2) # Find character function names
str1 = ' apples '
str2 = ' E '
result = Str1.index (str2) # finds characters in string str1 str2
Print (Result)

3. Copying strings

#strcpy (STR1,STR2) # Copy String function name
str1 = ' apples '
STR2 = str1 # Assigns the string str1 to str2
str1 = ' Bananas ' # re-assigning string to STR1
Print (STR2)

4. Connection string

#strcat (STR1,STR2) # connection string function name
str1 = ' apples '
str2 = ' Bananas '
STR1 + = str2 # string str1 concatenation with string str2 and assign to STR1
Print (STR1)

5. Uppercase and lowercase conversions in strings

Str.lower () #小写
Str.upper () #大写
Str.swapcase () #大小写互换
Str.capitalize () #首字母大写

#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 String function name
str = ' ABCDEFG '
str = str[::-1] # index from start to end, third parameter-1 for reverse
Print (str)

7. Finding strings

#strstr (STR1,STR2) # Find string function names
str1 = ' ABCDEFG '
str2 = ' CDE '
Print (Str1.find (str2)) # Find string in string str1 str2

#变量. Find ("What to Look for", [start position, end position]), start position and end position, indicate the range to look for, and empty means find all. The location is returned after finding it, the position is calculated from 0, and 1 is returned if not found.

8. String substitution

str = ' ABCABCABC '
Str=str.replace (' A ', ' 0 ') # replaces the character "a" in the string str with "0" and re-assigns the value to STR
Print (str)

9. String alignment

Str.ljust (N,[fillchar]) # output n characters, Str left-aligned, insufficient portions are filled with Fillchar, the default is a space.
Str.rjust (N,[fillchar]) # ibid., Align Right (n, [Fillchar]) # ibid., Center

10. Other functions

Str.count (char, [Start, [end]]) #计算字符char在字符串str中出现的次数, you can set where to start and where to end

Str.strip ([chars]) #把str中字符chars前后的字符全部去掉, can be understood to replace s before and after chars to 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]]) #以sep为分隔符, divides str into a list,maxsplit that represents the number of splits, and the default delimiter is a blank character

Str.startswith (char) #是否以char开头
Str.endswith (char) #以char结尾
Str.isalnum () #str是否全是字母和数字
Str.isalpha () #str是否全是字母
Str.isdigit () #str是否全是数字
Str.isspace () #str是否全是空白字符
Str.islower () #str中的字母是否全是小写
Str.isupper () #str中的字母是否便是大写
Str.istitle () #str是否是首字母大写的

11. String slicing

str = ' abcdefghigklm′
Str[0:3] #截取第一位到第三位的字符 # "ABC"
Str[:] #截取字符串的全部字符 # "ABCDEFGHIGKLM"            Br>str[6:] #截取第七个字符到结尾 # "HIGKLM"
Str[:-3] #截取从头开始到倒数第三个字符之前 # "Abcdefghig"
Str[2] #截取第三个字符 # "C"
Str[-1] #截取倒数第一个字符 # "M"
Str[::-1] #字符串倒序 # "MLK GIHGFEDCBA "
Str[-3:-1] #截取倒数第三位与倒数第一位之前的字符 #" KL "
Str[-3:] #截取倒数第三位到结尾 #" KLM "
Str [: -5:-3] #倒序截取后四位字符, and 2 characters to intercept # "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: 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.