Methods of STR class in Python (1)

Source: Internet
Author: User

capitalize () capitalize the first letter of the string

1 str1="ni hao ma"2 str2=str1.capitalize ()3  Print(STR2)
#输出: Ni Hao Ma

Center (width, fillchar=none) places the string in the middle, and fills it with the specified character at the set length.

str1=" This"str2=str1.center (20,"*")Print(STR2)#Output: ********this********str1=" This"str2=str1.center (5,"*")Print(STR2)#Output: *thisstr1=" This"str2=str1.center (6,"*")Print(STR2)#Output: *this*

Count (Sub, start=none, End=none) calculates the number of characters in a string

str1="This isstring"num=str1.count ("i") Print (num) # Output: 3

Decode (Encoding=none, Errors=none): Decoding

Encode (self, Encoding=none, Errors=none): encoded

EndsWith (self, suffix, start=none, end=none) decide whether to end with a character

str1="This isstring"num=str1.endswith ("ing")  print(num)# output: True

Expandtabs (self, tabsize=none) returns a tab character, tabsize this option specifies that the number of characters to be replaced by the "/h" default is 8

str1='this\tis\ta\tstring. ' str2=str1.expandtabs ()print(str2)# output: This    is      a       String.

Find (self, sub, Start=none, End=none) finds the position of the specified character in the string

str1='This isa string. ' # when found num=str1.find ("H")print(num)# Output: 1 # returned when not found-1num=str1.find ("x")print(num)  # output: 1

Format (*args, **kwargs) is similar to the use of%s, which is implemented by {}

str1='my name is {0},age {1}'num=str1.format ("Wang" ,"+")print(num)# output: My name is Wang, Age

Index (self, sub, Start=none, End=none) similar to find but if not found, the program will error

Isalnum (self) determines whether the string is all numbers and letters, and if true, returns false instead, noting that the space symbol returns false; only numbers or letters return true.

str1='my name is {0},age {1}'num=str1.isalnum ()print(num )# output: falsestr1='1aareawr23'num= Str1.isalnum () Print (num) # Output: True

Isalpha (self) determines whether the string is all letters, or returns TRUE if it is, otherwise false

IsDigit (self) determines whether the string is a number, or true if it is, otherwise false

Islower (self) determines whether the letters in the string are lowercase and returns false if True

1str1='WASD FDFDSA'2num=Str1.islower ()3 Print(num)4 5 #Output: True6 7str1='LAO Wang'8num=Str1.islower ()9 Print(num)Ten  One #Output: Flase

Isspace (self) determines whether the string is a space, or False if it returns true;

' '  = string.isspace ()print(new_str)# output: True'My Name is yue,my. '  = string.isspace ()print(new_str)# output: False

Istitle (self) determines whether all the first letters in the string are uppercase and returns False if True

' My Name is Yue. '  = string.istitle ()print(new_str)# output: True'My Name is yue,my. '  = string.istitle ()print(new_str)# output: False

Isupper (self) determines whether all letters in a string are uppercase or not returns true instead of returning false

' MY NAME is YUE. '  = string.isupper ()print(new_str)# output: True'My Name is Yue. '  = string.isupper ()print(new_str)# output: False

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

String = ("haha","lala","ohoh"  "-"print(Str.join (String))#  Output: Haha-lala-ohoh

Lower (self) converts all uppercase characters in a string to lowercase.

" My Name is YUE. " Print (String.Lower ()) # output: My name is Yue.

Ljust (self, width, fillchar=none) returns an original string left-aligned and fills a new string of the specified length with a space. Returns the original string if the specified length is less than the length of the original string.

" My name is Wang. " Print (String.ljust ()) # output: My name is Wang.                          

Lstrip (self, chars=none) truncates the left space of the string or the specified character

" My Name is YUE. " Print (String.lstrip ()) # output: My Name is YUE. " My Name is YUE. " Print (String.lstrip ('My')) # output: Name is YUE.

Partition (self, Sep) splits a string according to a specified delimiter

" http://www.mingyuanyun.com " Print (String.partition ('://')) # output: (' http ', '://', ' www.mingyuanyun.com ')

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

" My name is Yue. " Print (String.Replace ("Yue","ying")) # output: My name is ying.

Split (self, sep=none, maxsplit=none) slices the string by specifying a delimiter.

" haha lala gege " Print (String.Split (")) # output: [' haha ', ' lala ', ' Gege '] Print (String.Split (", 1 )) # output: [' haha ', ' lala Gege ']

Swapcase (self) converts the uppercase and lowercase letters of a string.

" My Name is Yue. " Print (String.swapcase ()) # output: MY NAME is YUE.

The title returns a "caption" string, meaning all words start with uppercase and the remaining letters are lowercase (see istitle ()).

" my name is yue,my. " Print (String.title ()) # output: My Name is yue,my.

Upper (self) converts lowercase letters in a string to uppercase.

" my name is yue,my. " Print (String.upper ()) # output: MY NAME is yue,my.

Zfill (self, width) returns a string of the specified length, the original string is right-aligned, and the front padding is 0.

" my name is Yue. " Print (String.zfill ()) # output: 000my name is Yue.

Translate (self, table, Deletechars=none) converts the character of a string based on the table given by the parameter table (containing 256 characters), and the character to be filtered out is placed in the Del parameter.

 from Import " aoeiu" "12345"=   " My name is Yue " Print (String.translate (trantab)) # output: My n1m3 4s y53

Methods of STR class in Python (1)

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.