A Python string

Source: Internet
Author: User

The encoding of strings in Python3 is Unicode. This means that the string can support Chinese.

Print (' Chinese ') #中文

There are many ways to manipulate strings in Python.

Capitalize first letter capital
name = ' Alex ' Print (Name.capitalize ()) #Alex
CountCount the number of characters in a string
name = ' Alex ' Print (Name.count (' a ')) #1
CenterReturns the center of an original string and fills it with the specified character to the length
name = ' Alex ' Print (Name.center, '-') #--------Alex--------
EndsWithDetermines whether a string ends with a specified character
name = ' Alex ' Print (Name.endswith (' ex ')) #True
Expandtabs to turn a newline in a tab into a space of a specified length
name = ' Hello \talex ' Print (Name.expandtabs (tabsize=30)) #hello                         Alex
Find string Lookup returns the position of the string to find in the search string
name = ' Hello Alex ' Print (Name.find (' Alex ')) #6
Format formatted output
name = ' My name is {name} ' Print (Name.format (name= ' Alex ')) #my name is Alex
Format_map formatted output a dictionary is passed.
name = ' My name is {name} ' Print (Name.format_map ({' name ': ' Alex '}) "#my name is Alex
Isalnum to determine whether a string is only Arabic letters or numbers
name = ' My name is {name} ' Print (Name.isalnum ()) #Falsename1 = ' 1mynameisname ' Print (Name1.isalnum ()) #True
Isalpha determines whether a string is a plain English character
name1 = ' 1mynameisname ' Print (Name1.isalpha ()) #Falsename2 = ' mynameisname ' Print (Name2.isalpha ()) #True
IsDigitDetermines whether a string is an integer
name2 = ' mynameisname ' Print (Name2.isdigit ()) #Falsename3 = ' 222 ' Print (Name3.isdigit ()) #True
IslowerDetermine if a string is lowercase
name2 = ' My name is Alex ' Print (Name2.islower ()) #Falsename3 = ' My name is Alex ' Print (Name3.islower ()) #True
Join turns a list into a string and splits it by the specified delimiter
Print (', '. Join ([' 1 ', ' 2 ', ' 3 ')) #1, 2,3
Lower uppercase to lowercase
Print (' ALEX '. Lower ()) #alex
Upper lowercase to uppercase
Print (' Alex '. Upper ()) #ALEX
Lstrip to the left space or carriage return
Print (' \nalex '. Lstrip ()) #alex
Rstrip go to the right or enter the blanks
Print (' alex\n '. Rstrip ()) #alex
Strip go to either side space or enter
Print (' \nalex\n '. Strip ()) #alex
Translate

Grammar:

Str.translate (table[, Deletechars]);

The Python translate () method converts the character of a string according to the table given by the parameter table (containing 256 characters), and the character to be filtered out is placed in the Del parameter.

A = Str.maketrans (' abcdef ', ' 123456 ') print (' Alex Li '. Translate (a)) #1l5x Li
Replace string substitution
Print (' Alex '. Replace (' A ', ' B ')) #blex
Split string Split list
Print (' Alex_li '. Split ('_')) #[' Alex ', ' Li ']
Splitlines split string to list by line break
Print (' Alex\nli '. Splitlines ()) #[' Alex ', ' Li ']

A Python string

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.