An example of the function of the Python3 string is explained in detail

Source: Internet
Author: User
Tags translate function
Add function (Append string after)

S1 = ' Hello ' s2 = s1.__add__ (' boy! ') Print (s2) #输出: Hello boy!

Contains (determines whether a string is included, contains returns True)

S1 = ' Hello ' result = s1.__contains__ (' He ') print (Result) #输出: True

EQ (determines if two strings are the same, returns true if the same)

S1 = ' Hello ' s2 = ' how ' result = s1.__eq__ (s2) print (Result) #输出: False

Format

#占位

GetAttribute

#占位

GetItem

#占位

Getnewargs

#占位

GE (greater than or equal to)

Print (' B '. GE (' a ')) #输出: True

GT (Greater than)

Print (' B '. GE (' a ')) #输出: True

Hash

#占位

Iter

#占位

Len (return string length)

Print (' abc '. Len ()) #输出: 3

Le (less than or equal to)

Print (' B '. Le (' a ')) #输出: False

LT (less than)

Print (' B '. Lt (' a ')) #输出: False

MoD

#占位

Mul

#占位

New

#占位

Ne

#占位

Repr

#占位

Rmod

#占位

Rmul

#占位

sizeof

#占位

STR (return to self)

Print (' abc '. __STR__ ()) #输出: ABC

Capitalize (first letter capital)

s = ' tom ' Print (S.capitalize ()) #输出: Tom

Casefold (uppercase converted to lowercase)

s = ' tom ' Print (S.casefold ()) #输出: Tom

Center (specify length and padding characters, center content, fill word Fuliushi space)

s = ' tom ' Print (S.center, '-') #输出:--------Tom---------

Count (calculates the number of occurrences of a string, the second argument: start position, third parameter: End position)

s = ' AABBBCCCCCDD ' Print (S.count (' cc ', 3,11)) #输出: 2

Encode (coded)

s = "Chinese" Print (S.encode (' GBK ')) #输出: B ' \xd6\xd0\xce\xc4 '

EndsWith (determines whether a string ends with a character or string, the second argument: start position, third parameter: End position)

s = ' Projects ' Print (S.endswith (' ts ')) print (S.endswith (' e ', 0,5)) #输出: true#     True

Expandtabs (convert 1 tab keys to 7 spaces)

s = ' h\ti ' Print (S.expandtabs ()) #输出: H       I

Find (finds the index position of a character or string, the second parameter: Start position, third parameter: End position)

s = ' Hello ' Print (S.find (' o ')) print (S.find (' O ', 0,3))  #找不到返回 -1# output: 4#     -1

Format (string formatting/splicing)

name = ' Tom ' age = 18s = ' {0}\ ' 's age is {1} '. Format (name,age) print (s) #或者str = ' {name}\ ' 's age was {age} ' result = Str.format (a Ge=18,name= ' Tom ') print (Result) #输出: Tom ' s age is 18

Format_map

#占位

Index (finds the indexed position of a character or string, unlike find, if the character does not exist, it will be an error)

s = ' Hello ' Print (S.index (' o ')) print (S.index (' e ', 0,3)) #输出: 4#     1

Isalnum (whether it is a letter or a number)

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

Isalpha (whether it is a letter)

s = ' 123 ' Print (S.isalpha ()) #输出: False

Isdecimal (whether it is a decimal number)

s = ' 123 ' Print (S.isdecimal ()) #输出: True#true:unicode number, full-width digit (double byte) #False: Roman numerals, Chinese numerals #error:byte digits (single byte)

IsDigit (whether a number)

s = ' 123 ' Print (S.isdigit ()) #输出: true#true:unicode number, byte digit (single byte), Full width digit (double byte), Roman numeral #false: Chinese numerals

Isidentifier (whether identifier/variable name)

s = ' 1num ' Print (S.isidentifier ()) #输出: false# Because the variable name cannot start with a number

Islower (whether all lowercase letters)

s = ' Hello ' Print (S.islower ()) #输出: False

IsNumeric (whether a number)

s = ' 123 ' Print (S.isnumeric ()) #输出: True#true:unicode digits, full-width numerals (double-byte), Roman numerals, Chinese numerals

isprintable (whether printable character/can output as-is)

s = ' \ n ' Print (s.isprintable ()) #输出: False

Isspace (whether it is a space)

Print (". Isspace ()) print (' \ t '. Isspace ()) #输出: true#     True

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

Print (' Hello Boy ', Istitle ()) print (' Hello Boy '. Istitle ()) #输出: true#     False

Isupper (whether all uppercase letters)

Print (' Boy ', Isupper ()) print (' Boy '. Isupper ()) #输出: true#     False

Join (generates a new string for the elements in the sequence with the specified character connection)

s = [' H ', ' e ', ' l ', ' l ', ' O ']print ('. Join (s)) ' Print ('-'. Join ') #输出: hello#     H-e-l-l-o

Ljust (specify length and padding characters, content left-aligned, padding word Fuliushi is a space)

s = ' Hello ' print (S.ljust (ten, '-')) #输出: Hello-----

Lower (all strings converted to lowercase)

s = ' tom ' Print (S.lower ()) #输出: Tom

Lstrip (removes the specified character to the left of the string, the default is a space)

s = '   tom ' Print (S.lstrip ()) #输出: Tom

Maketrans (Create a conversion table of character mappings, use with the Translate function)

Intab = "ABCDE" Outtab = "12345" Trantab = Str.maketrans (intab, outtab) str = "Hello abc" Print (Str.translate (trantab)) #输出: H 5llo 123

Partition (Specify delimiter, split string)

s = ' iamtom ' Print (s.partition (' AM ')) #输出: (' I ', ' am ', ' Tom ')

Replace (replaces the old string in the string with the new one), and if you specify the third parameter max, the replacement does not exceed Max times. )

s = ' Tom ' Print (S.replace (' m ', ' O ') #输出: Too

RFind (finds the location of the specified string from the right, and returns 1 if no match is found)

s = ' one-one ' print (S.rfind (' one ')) print (S.rfind (' One ', 0,6))  #指定起始和结束位置 # output: 8#     0

Rindex (finds the location of the specified string from the right, if no match is present)

s = ' one-one ' print (S.rindex (' one ')) print (S.rindex (' One ', 0,6))  #指定起始和结束位置 # output: 8#     0

Rjust (Specifies the length and padding characters, right-aligned content, padding character Fuliushi, or spaces)

s = ' Hello ' print (S.rjust (ten, '-')) #输出:-----Hello

Rpartition (Specifies the delimiter, starting from the right to split the string)

s = ' iamtom_iamtom ' Print (s.rpartition (' AM ')) #输出: (' iamtom_i ', ' am ', ' Tom ')

Rsplit (Specifies the delimiter to slice the string, and if you specify num for the second parameter, only num times are separated and a list is returned)

s = ' A b c d ' Print (S.rsplit ()) Print (S.rsplit (', 2))  #从右边开始, separated by spaces two times # output: [' A ', ' B ', ' C ', ' d ']#     [' A B ', ' C ', ' d ']

Rstrip (removes the specified character at the end of the string, the default is a space)

s = '!!! I am TOM!!! ' Print (S.rstrip ('! ')) #输出:!!! I am Tom

Split (Specifies the delimiter to slice the string, and if you specify num for the second parameter, only num is separated, and then a list is returned)

s = ' A b c d ' Print (S.split ()) Print (S.split (', 2)) #从左边开始, separated by spaces two times # output: [' A ', ' B ', ' C ', ' d ']#    [' A ', ' B ', ' C d ']

Splitlines (separates strings by newline character, returns a list)

s = ' a\nb\nc ' Print (S.splitlines ())    #默认参数为Falseprint (S.splitlines (True)) #指定Ture参数 preserves line break # output: [' A ', ' B ', ' C ']#     [' a\n ', ' b\n ', ' C ']

StartsWith (Determines whether a string begins with a character or string, the second argument: start position, third parameter: End position)

s = ' Projects ' Print (S.startswith (' Pr ')) print (S.startswith (' e ', 4,8)) #输出: true#     True

Strip (The specified character before and after the string is deleted, the default is a space)

s = '!!! I am TOM!!! ' Print (S.strip ('! ')) #输出: I am Tom

Swapcase (case swap)

s = ' I Am Tom ' Print (S.swapcase ()) #输出: I am Tom

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

s = ' I Am Tom ' Print (S.title ()) #输出: I am Tom

Translate (character substitution based on the table created by the Maketrans method)

Intab = "ABCDE" Outtab = "12345" Trantab = Str.maketrans (intab, outtab) str = "Hello abc" Print (Str.translate (trantab)) #输出: H 5llo 123

Upper (lowercase converted to uppercase)

s = ' Hello ' print (S.upper ()) #输出: Hello

Zfill (Specifies the length of the string. Original string right-aligned, front padding 0)

s = ' Hello ' Print (S.zfill (10)) # Output: 00000Hello
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.