Python string manipulation (remove space strip (), slice, find, join join (), split split (), convert first letter uppercase, convert letter case ... )

Source: Internet
Author: User


#可变变量: List, dictionary
#不可变变量: Meta-ancestor, string
Manipulation of strings (remove spaces, slices, find, join, split, convert first letter uppercase, convert letter case, determine if it is a numeric letter, member operator (In/not in))
strings are enclosed in single or double quotes: var1 = ' Hello ' var2 = ' World '

The string cannot modify the value, as shown in the following example:

The following are some of the common operations of strings.

1. Remove spaces
Str.strip () # Remove both spaces and line breaks
Str.strip (' a ') # Remove the characters specified on both sides
Str.lstrip () #去掉左边的空格
Str.rstrip () #去掉右边的空格



2. Find
Access string, you can get the string by using square brackets to find an index, s[0] # 1th
To truncate a string, S[2:-1] # 2nd to last
Find out if a character is present, S.find (' B ') # finds the index that returns B, returns the first one, and cannot find the return-1
Find out if there is a character, another way to use Index (), S.index (' B ') to find the index of return B, more than the first one, the error is not found
Find the number of characters, S.count (' a ')

3. Connect (with join () to concatenate list, tuple,dict to string)

4. Segmentation(spilt () splits the string into list)

5. Convert letter case (capitalized), all uppercase letters, all letters lowercase, print a string with style, ... To begin with; End, string substitution
Str.capitalize () #首字母大写
Str.upper () # turn all the letters into uppercase

Str.lower () # turn all the letters into lowercase
Str.center ((), '-') #字符串放中间, both sides-padded
Sql.startswith (' select ') # starts with select
File_name.endswith ('. xls ') # ends with. xls
Sql.replace (' Select ', ' Update ') #字符串替换, old--new

6. String Formatting (F.format (), F.format_map ())

7. Determine if it is a number, letter, space, \ t, \ n, \ r

8. In/not in (Member operator)

In: Member operator-returns TRUE if the string contains the given character

Not in: Member operator-returns TRUE if the string does not contain the given character

#in/not in
If I in D:
Pass
If I not in D:
Pass
Print (' A ' in ' ACDA ') #-Ture
Print (' A ' not in ' AFDSHJW ') #-False

Python string manipulation (remove space strip (), slice, find, join join (), split split (), convert first letter uppercase, convert letter case ... )

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.