Usage tutorial of str (string) in python3, python3str

Source: Internet
Author: User
Tags printable characters translate function

Usage tutorial of str (string) in python3, python3str

This article mainly describes how to use str (string) in python3. This article describes the operations in detail. Let's take a look at them.

_ Add _ function (append a string to the end)

S1 = 'hello' s2 = s1. _ add _ ('Boy! ') Print (s2) # output: Hello boy!

_ Contains _ (determines whether a string is contained, and returns True if it contains)

S1 = 'hello' result = s1. _ contains _ ('hes') print (result) # output: True

_ Eq _ (returns True if two strings are the same)

S1 = 'hello' s2 = 'who' result = s1. _ eq _ (s2) print (result) # output: False

_ Format __

# Placeholder

_ Getattribute __

# Placeholder

_ Getitem __

# Placeholder

_ Getnewargs __

# Placeholder

_ Ge _ (greater than or equal)

Print ('B'. _ ge _ ('A') # output: True

_ Gt _ (greater)

Print ('B'. _ ge _ ('A') # output: True

_ Hash __

# Placeholder

_ Iter __

# Placeholder

_ Len _ (returns the string length)

Print ('abc'. _ len _ () # output: 3

_ Le _ (less than or equal)

Print ('B'. _ le _ ('A') # output: False

_ Lt _ (less)

Print ('B'. _ lt _ ('A') # output: False

_ Mod __

# Placeholder

_ Mul __

# Placeholder

_ New __

# Placeholder

_ Ne __

# Placeholder

_ Repr __

# Placeholder

_ Rmod __

# Placeholder

_ Rmul __

# Placeholder

_ Sizeof __

# Placeholder

_ Str _ (return user)

Print ('abc'. _ str _ () # output: abc

Capitalize (uppercase)

S = 'Tom 'print (s. capitalize () # output: tom

Casefold (convert uppercase to lowercase)

S = 'Tom 'print (s. casefold () # output: TOM

Center (specify the length and fill characters, center the content, and leave blank fill characters as spaces)

S = 'Tom 'print (s. center (20,'-') # output: -------- Tom ---------

Count (calculate the number of occurrences of a string. The second parameter is the start position and the third parameter is the end position)

S = 'aabbbcccdd' print (s. count ('cc', 3,11) # output: 2

Encode)

S = "Chinese" print (s. encode ('gbk') # output: B '\ xd6 \ xd0 \ xce \ xc4'

Endswith (judge whether the string ends with a certain character or string. The second parameter is the start position and the third parameter is the end position)

S = 'project' print (s. endswith ('ts') print (s. endswith ('E',) # output: True # True

Expandtabs (convert one tab key to seven spaces)

S = 'H \ ti' print (s. expandtabs () # output: H I

Find (search for the index location of a character or string, the second parameter: Start position, the third parameter: end position)

S = 'hello' print (s. find ('O') print (s. find ('O',) # returns-1 if not found # outputs: 4 #-1

Format (string formatting/splicing)

Name = 'Tom 'age = 18 s =' {0} \'s age is {1 }'. format (name, age) print (s) # Or str = '{name} \'s age is {age}' result = str. format (age = 18, name = 'Tom ') print (result) # output: Tom's age is 18

Format_map

# Placeholder

Index (query the index location of a character or string, which is different from find. If the character does not exist, an error is returned)

S = 'hello' print (s. index ('O') print (s. index ('E',) # output: 4 #1

Isalnum (whether it is a letter or number)

S = '! # 'Print (s. isalnum () # output: False

Isalpha (letter or not)

S = '000000' print (s. isalpha () # output: False

Isdecimal (whether it is a decimal number)

S = '000000' print (s. isdecimal () # output: True # True: Unicode number, full-angle number (dual-byte) # False: Roman numerals, Chinese characters # Error: byte number (single-byte)

Isdigit (whether it is a number)

S = '000000' print (s. isdigit () # output: True # True: Unicode number, byte number (single-byte), full-byte number (double-byte), and Rome number # False: Chinese Character number

Isidentifier (whether it is an identifier/variable name)

S = '1num' print (s. isidentifier () # output: False # because the variable name cannot start with a number

Islower (whether all are lowercase letters)

S = 'hello' print (s. islower () # output: False

Isnumeric (whether it is a number)

S = '000000' print (s. isnumeric () # output: True # True: Unicode number, full-angle number (dubyte), roman number, Chinese Character number

Isprintable (whether it is printable characters/whether it can be output as is)

S = '\ n' print (s. isprintable () # output: False

Isspace (whether it is a space)

Print (''. isspace () print ('\ t'. isspace () # output: True # True

Istitle (whether it is the title/uppercase of the first letter of each word)

Print ('Hello Boy '. istitle () print ('Hello boy'. istitle () # output: True # False

Isupper (whether all are uppercase letters)

Print ('Boy '. isupper () print ('Boy'. isupper () # output: True # False

Join (Concatenates the elements in the sequence with specified characters to generate a new string)

S = ['h', 'E', 'l', 'l', 'O'] print (''. join (s) print ('-'. join (s) # output: Hello # H-e-l-o

Ljust (specify the length and fill characters. The content is left aligned. If the fill character is left blank, It is a space)

S = 'hello' print (s. ljust (10, '-') # output: Hello -----

Lower (replace all strings with lowercase letters)

S = 'Tom 'print (s. lower () # output: TOM

Lstrip (remove the character specified on the left of the string. The default value is space)

S = 'Tom 'print (s. lstrip () # output: Tom

Maketrans (create a conversion table for character ing and use it with the translate function)

Intab = "abcde" outtab = "12345" trantab = str. maketrans (intab, outtab) str = "Hello abc" print (str. translate (trantab) # output: H5llo 123

Partition (specify a separator to separate strings)

S = 'amtom 'print (s. partition ('am') # output :(' I ', 'am', 'Tom ')

Replace (replace old (old string) in the string with new (new String). If the third parameter max is specified, replace it with max no more than once .)

S = 'Tom 'print (s. replace ('M', 'O') # output: Too

Rfind (find the position where the specified string appears from the right side. If no match exists,-1 is returned)

S = 'one two one' print (s. rfind ('one') print (s. rfind ('one',) # specify the start position and end position # output: 8 #0

Rindex (locate the position where the specified string appears from the right side. If no matching item exists, an error is returned)

S = 'one two one' print (s. rindex ('one') print (s. rindex ('one',) # specify the start position and end position # output: 8 #0

Fill ust (specify the length and fill characters. The content is right aligned. If the fill character is left blank, It is a space)

S = 'hello' print (s. Rows ust (10, '-') # output: ----- Hello

Rpartition (specify the separator to split the string from the right)

S = 'iamtom _ IamTom 'print (s. rpartition ('am') # output :( 'iamtom _ I', 'am', 'Tom ')

Rsplit (specify the delimiter to slice the string. If the second parameter num is specified, only num is separated and a list is returned)

S = 'a B c d' print (s. rsplit () print (s. rsplit ('', 2) # Start from the right and separate them by spaces twice # output: ['A', 'B', 'C ', 'D'] # ['a B ', 'C', 'D']

Rstrip (delete the specified character at the end of the string, which is a space by default)

S = '!!! I am Tom !!! 'Print (s. rstrip ('! ') # Output :!!! I am Tom

Split (specify the delimiter to slice the string. If the second parameter num is specified, only num is separated and a list is returned)

S = 'a B c d' print (s. split () print (s. split ('', 2) # Start from the left and separate them by space twice # output: ['A', 'B', 'C ', 'D'] # ['A', 'B', 'c d']

Splitlines (returns a list of strings separated by linefeeds)

S = 'a \ nb \ nc 'print (s. splitlines () # The default parameter is Falseprint (s. splitlines (True) # If the True parameter is specified, the linefeed is retained. # output: ['A', 'B', 'C'] # ['a \ n ', 'B \ n', 'C']

Startswith (determines whether the string starts with a certain character or string. The second parameter is the start position and the third parameter is the end position)

S = 'project' print (s. startswith ('prs') print (s. startswith ('E', 4, 8) # output: True # True

Strip (delete the specified character before and after the string, the default is space)

S = '!!! I am Tom !!! 'Print (s. strip ('! ') # Output: I am Tom

Swapcase (case-insensitive)

S = 'I am Tom' print (s. swapcase () # output: I AM tOM

Title (converted to the title, that is, the first letter of each word is capitalized)

S = 'I am tom' print (s. title () # output: I Am Tom

Translate (replace the characters in the table created based on the maketrans method)

Intab = "abcde" outtab = "12345" trantab = str. maketrans (intab, outtab) str = "Hello abc" print (str. translate (trantab) # output: H5llo 123

Upper (lowercase to uppercase)

S = 'hello' print (s. upper () # output: Hello

Zfill (specify the length of the string. Right alignment of the original string, with the front filled with 0)

S = 'hello' print (s. zfill (10) # output: 00000 Hello

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.