Python3 string function example, python3 string example

Source: Internet
Author: User
Tags printable characters

Python3 string function example, python3 string example

_ 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 (the first parameter specifies the length, and the second parameter specifies the characters to be filled. If it is left blank, It is a space)

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: Start position, and the third parameter: 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 (determines whether the string is a letter or number)

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

Isalpha (determines whether the string is a letter)

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

Isdecimal (judge whether the string is in decimal format)

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 (judge whether the string 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 (judge whether the string is an identifier/variable name)

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

Islower (determines whether all strings are in lower case)

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

Isnumeric (judge whether the string 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

 

 

 

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.