Python string built-in function eight

Source: Internet
Author: User
Tags uppercase letter

This is the content of the December plan, beginning in January 2018 ~

If there is any mistake, please ask for it ~

Letter Manipulation

str.capitalize()

Returns a copy of the string with the first character capitalized and the remaining characters lowercase.

str.title()

Returns a version of the first letter of the string, with all words starting with an uppercase letter and the remaining letters lowercase.

1 string="abcD"2print(String.capitalize ())  #ABCD

str.casefold()

returns a copy of the string. and Lower () are converted to lowercase functions, but casefold can be converted to non-Chinese English case (but usually said the size of the word seems to refer to the English bar), in addition to the Str.upper () also written here.

1 string_1="AbcD"2print(String_1.casefold ()) # ABCD

str.center(Width[, Fillchar])

Returns a string with a width of length and centers itself. fills are completed using the specified Fillchar (the default is ASCII whitespace). if width is less than or equal, len(s) the original string is returned.

1 string_2="a"2 string="abcD"3 Print (String_2.center,'*')) 4 Print (String.center (3,'*'))
a*****AbcD

str.swapcase()

Uppercase and lowercase swaps

str. isalnum () returns True if all characters in the string are alphanumeric and has at least one character, otherwise false

str.isalpha()如果字符串中的所有字符都是字母并且至少有一个字符,则返回true,否则返回false。

str. isdigit () returns true if all characters in the string are numbers and at least one character, otherwise false.

str.islower()如果字符串中的所有字符都小写,并且至少有一个字符,则返回true,否则返回false。

str. isupper () returns true if all characters in the string are uppercase and has at least one character, otherwise false is returned.

str. isspace () returns true if there is only a space character in the string and has at least one character, otherwise false is returned.

String manipulation

tr.count(Sub[, Start[, End]])

Returns the number of occurrences of a substring sub that is not overlapping in the [Start, end] range.

 1  string_3= " AABBBCC   " 2  print  (String_3.count ( " b  "   3  print  (String_3.count ( " b  " , 1,4  4  #   3  5   2 

str.encode(encoding= "Utf-8", errors= "strict")

Returns the encoded version of the string as a byte object. The default encoding is ‘utf-8‘ . errors can be given to set different error handling scenarios. The default value for errors ‘strict‘ is

1string_4="Cherry blossom Fall Dance"2 Print(String_4.encode ('Utf-8'))3 #b ' \xe6\xa8\xb1\xe8\x8a\xb1\xe8\x90\xbd\xe8\x88\x9e '4 Print(String_4.encode ('Utf-8'). Decode ('Utf-8'))5 #Cherry blossom Fall Dance

str.endswith(Suffix[, start[, End]])

If the string is returned at the end of the specified suffix False , otherwise it is returned True . suffix can also be a tuple. An optional start indicates that the test starts at that location. An optional end indicates that the comparison is stopped at that location.

str.startswith(Prefix[, start[, End]])

Returns if the string starts with prefix True False .

1string="AbcD!"2Endstr='!'3 Print(String.endswith (ENDSTR))4 Print(String.endswith (endstr,0,4))5 Print(String.endswith (endstr,0))6 #True7 #False8 #True

str.expandtabs(tabsize=8)

If the character is a tab ( \t ), insert one or more space characters in the result until the current column is equal to the next tab position. Well, that's a good example.

str.find(Sub[, Start[, End]])

Finds the lowest index in the string of sub-string sub, returns -1   If no sub is found. If we just know if it exists, use in

str.rfind(Sub[, Start[, End]])

Returns the last occurrence of the searched substring at the index position of the string

Code See crawl B station cover

1 keystr.find ('(')

str.format(*args, **kwargs)

The format method is used for the formatted output of the string

How to use

Https://www.cnblogs.com/Alexzzzz/p/6832253.html

str.replace(Old, new[, Count])

All of the substring old is replaced by new. If an optional parameter count is specified, only the preceding count occurrences are replaced.

1string="aaaaaa"2 Print(String.Replace ('a','b'))3 Print(String.Replace ('a','b', 3))4 bbbbbb5Bbbaaa

Python string built-in function eight

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.