Description of the "Python" Str class method

Source: Internet
Author: User

#capitalize (): string first character uppercase

String = ' This is a string. '
New_str = String.capitalize ()
Print (NEW_STR)
#输出: This is a string.

#center (width, fillchar=none): Places the string in the middle, at the specified length, with the specified character padding
String = ' This is a string. '
New_str = String.center (30, ' * ')
Print (NEW_STR)
#输出: ******this is a string.*******

#count (Sub, Start=none, End=none): Calculates the number of characters in a string
String = ' This is a string. '
New_str = String.count (' i ')
Print (NEW_STR)
#输出: 3

#decode (Encoding=none, Errors=none): Decoding
String = ' This is a string. '
New_str = String.decode ()
Print (NEW_STR)

#encode (self, Encoding=none, Errors=none): encoded
String = ' This is a string. '
New_str = String.encode ()
Print (NEW_STR)

#endswith (self, suffix, start=none, end=none): Decide whether to end with a character
String = ' This is a string. '
New_str = String.endswith (' ing. ')
Print (NEW_STR)
#输出: True
New_str = String.endswith (' xx ')
Print (NEW_STR)
#输出: False

#expandtabs (self, Tabsize=none): Returns a tab character. Tabsize This option specifies the number of characters to be replaced with the tab "\ T", which defaults to 8
String_expandtabs = ' this\tis\ta\tstring. '
New_str = String_expandtabs.expandtabs ()
Print (NEW_STR)
#输出: This is a string.

#find (self, sub, Start=none, End=none): Find the position of the specified character in a string
String = ' This is a string. '
New_str = String.find (' a ') #找的到的情况
Print (NEW_STR)
#输出: 8
New_str = String.find (' xx ') #找不到的情况返回-1
Print (NEW_STR)
#输出:-1

#format (*args, **kwargs): Similar to the use of%s, it is implemented by {}
string1 = ' My name is {0},my job ' is {1}. '
NEW_STR1 = String1.format (' Yue ', ' tester ')
Print (NEW_STR1)
#输出: My name is yue,my job is tester.
string2 = ' My name is {name},my job ' is {job}. '
NEW_STR2 = String2.format (name= ' Yue ', job= ' tester ')
Print (NEW_STR2)
#输出: My name is yue,my job is tester.

#index (self, sub, Start=none, End=none): similar to find
String = ' This is a string. '
New_str = String.index (' a ') #找的到的情况
Print (NEW_STR)
#输出: 8
New_str = String.index (' xx ') #找不到的情况, program error
Print (NEW_STR)
#输出: Program run error, valueerror:substring not found

#isalnum (self): Determines whether the string is both a number and a letter, and returns true if yes, otherwise false
string = ' My name is yue,my. 18. '
New_str = String.isalnum ()
Print (NEW_STR)
#输出: False
string = ' Haha18121314lala '
New_str = String.isalnum ()
Print (NEW_STR)
#输出: True

#isalpha (self): Determines whether the string is all letters, or returns TRUE if it is false otherwise
string = ' ABCDEFG '
New_str = String.isalpha ()
Print (NEW_STR)
#输出: True
string = ' My name is Yue '
New_str = String.isalpha () #字母中间带空格, special characters are not
Print (NEW_STR)
#输出: False

# isdigit (self): Determines whether the string is a number, or true if it is, otherwise false
string = ' 1234567890 '
New_str = String.isdigit ()
Print (NEW_STR)
#输出: True
string = ' Haha123lala '
New_str = String.isdigit () #中间带空格, special characters are not
Print (NEW_STR)
#输出: False

# Islower (self): Determines whether the letters in the string are lowercase, returns true if yes, otherwise returns false
string = ' My name is yue,my. 18. '
New_str = String.islower ()
Print (NEW_STR)
#输出: True
string = ' My name is yue,my. 18. '
New_str = String.islower ()
Print (NEW_STR)
#输出: False

# isspace (self): Determines whether the string is a space, returns true if it is, or false
string = ' '
New_str = String.isspace ()
Print (NEW_STR)
#输出: True
string = ' My name is yue,my. 18. '
New_str = String.isspace ()
Print (NEW_STR)
#输出: False

# Istitle (self): detects if all the words in the string are spelled in uppercase, and the other letters are lowercase.
string = ' My Name is Yue. '
New_str = String.istitle ()
Print (NEW_STR)
#输出: True
string = ' My name is yue,my. 18. '
New_str = String.istitle ()
Print (NEW_STR)
#输出: False

# Isupper (self): detects whether all letters in the string are uppercase.
string = ' MY NAME is YUE. '
New_str = String.isupper ()
Print (NEW_STR)
#输出: True
string = ' My name is Yue. '
New_str = String.isupper ()
Print (NEW_STR)
#输出: False

# Join (Self, iterable): Generates a new string from the elements in the sequence with the specified character connection.
String = ("haha", "lala", "Ohoh")
str = "-"
Print (Str.join (string))
#输出: Haha-lala-ohoh

# 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.
string = "My name is Yue."
Print (String.ljust (18))
#输出: My name is Yue.

# lower (self): Converts all uppercase characters in a string to lowercase.
String = "My Name is YUE."
Print (String.Lower ())
# output: My name is Yue.

# Lstrip (self, chars=none): Truncates the left space of the string or specifies the character.
String = "My Name is YUE."
Print (String.lstrip ())
#输出: My Name is YUE.
String = "My Name is YUE."
Print (String.lstrip (' My '))
#输出: Name is YUE.

# partition (self, SEP): Splits the string according to the specified delimiter.
string = "Http://www.mingyuanyun.com"
Print (String.partition ('://'))
#输出: (' http ', '://', ' www.mingyuanyun.com ')

#replace (self, old, new, Count=none): replaces the older string in the string with the new one, and if you specify the third parameter max, the replacement does not exceed Max times.
string = "My name is Yue."
Print (String.Replace ("Yue", "Ying"))
#输出: My name is Ying.

# RFind (self, sub, Start=none, End=none): Returns the position of the last occurrence of the string, or 1 if there is no match.
string = "My name is Yue."
Print (String.rfind (' is '))
#输出: 8
string = "My name is Yue."
Print (String.rfind (' XXX '))
#输出:-1

# Rindex (self, sub, Start=none, End=none): Returns the substring of STR in the last occurrence of the string, and if there is no matching string to report the exception, you can specify an optional parameter [Beg:end] to set the search interval.
string = "My name is Yue."
Print (String.rindex (' is '))
#输出: 8
string = "My name is Yue."
Print (String.rindex (' XXX '))
#输出: Valueerror:substring not found

# Rjust (self, Width, fillchar=none): Returns the right alignment of an original string and fills the new string with a space of length width. Returns the original string if the specified length is less than the length of the string.
string = "My name is Yue."
Print (String.rjust (18))
#输出: My name is Yue.

# Rpartition (Self, SEP): Splits the string from right to the specified delimiter.
string = "Http://www.mingyuanyun.com"
Print (String.rpartition ('. '))
#输出: (' Http://www.mingyuanyun ', '. ', ' com ')

# Split (self, Sep=none, Maxsplit=none): Slices The string by specifying a delimiter.
string = "haha lala gege"
Print (String.Split ("))
#输出: [' haha ', ' lala ', ' Gege ']
Print (String.Split (', 1))
#输出: [' haha ', ' lala Gege ']

# Rsplit (self, Sep=none, Maxsplit=none): Slices the string from right by specifying a delimiter.
string = "haha lala gege"
Print (String.rsplit ("))
#输出: [' haha ', ' lala ', ' Gege ']
Print (String.rsplit (', 1))
#输出: [' haha lala ', ' Gege ']

# Rstrip (self, chars=none): Deletes the specified character at the end of the string (the default is a space).
string = "My name is Yue."
Print (String.rstrip ())
#输出: My name is Yue.

# strip (Self, chars=none): Removes the character specified by the tail of the string (the default is a space).
string = "My name is Yue."
Print (String.strip ())
#输出: My name is Yue.

# Splitlines (self, keepends=false): Separated by line (' \ r ', ' \ r \ n ', \ n '), returns a list containing the rows as elements, if the argument keepends is False, does not contain a newline character, if true, The line break is preserved.
str1 = ' ab C\n\nde fg\rkl\r\n '
Print (Str1.splitlines ())
# output: [' ab C ', ', ' de fg ', ' KL ']
str2 = ' ab C\n\nde fg\rkl\r\n '
Print (Str2.splitlines (True))
# output: [' ab c\n ', ' \ n ', ' de fg\r ', ' kl\r\n ']

# startswith (self, prefix, Start=none, End=none): Checks whether the string starts with the specified substring, returns True if yes, otherwise returns FALSE. If the parameter beg and end specify a value, the check is within the specified range.
string = "My name is Yue."
Print (String.startswith (' My '))
#输出: True
string = "My name is Yue."
Print (String.startswith (' Yue '))
#输出: False

# swapcase (self): Converts the uppercase and lowercase letters of a string.
String = "My Name is Yue."
Print (String.swapcase ())
#输出: MY NAME is YUE.

# title: Returns a "header" string, meaning that all words start with uppercase and the remaining letters are lowercase (see istitle ()).
string = "My name is Yue,my 18."
Print (String.title ())
#输出: My Name is yue,my 18.

# Translate (self, table, Deletechars=none): 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 into the del parameter.
From string import Maketrans
str = "AOEIU"
num = "12345"
Trantab = Maketrans (str, num)
string = "My name is Yue"
Print (String.translate (trantab))
# output: my n1m3 4s y53

# upper (self): Converts lowercase letters in a string to uppercase.
string = "My name is Yue,my 18."
Print (String.upper ())
#输出: MY NAME is yue,my 18.

# Zfill (self, width): Returns a string of the specified length, the original string is right-aligned, and the front padding is 0.
string = "My name is Yue."
Print (String.zfill (18))
#输出: 000my name is Yue.

Description of the "Python" Str class method

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.