Python learning process (8) string built-in function

Source: Internet
Author: User

The python string built-in function implements most of the methods of the string module and includes support for Unicode encoding methods.

(1) Capitalize ():

The first letter of the string is capitalized, and the other letters are lowercase. For 8-bit byte encoding needs to be based on the local environment.

>>> str='I AM menangel!  '+'I am student! ' Print (str)      I AM menangel! I am student! >>> str.capitalize ()'I am menangel! I am student! '

(2) Center (width):

Returns the center of the original string and fills the new string with a space of length width. The default padding character is a space.

>>> str='Title:python and Big data!  '>>> str'Title:python and Big dat!  '>>> str.center (+)'       Title:python and Big dat!         '

(3) Count (Sub, start= 0,end=len (String)):

Used to count the occurrences of a character in a string. The optional parameters are the start and end positions of the string search.

>>> str='xiaoming and Menangel and Xiaohua'>>> str.count ('  and', 10,20) 0>>> str.count ('and', 0,len (str)) 2

(4) Encode (encoding= ' UTF-8 ', errors= ' strict '):

Encodes a string in the encoding format specified by encoding. The default encoding is string encoding. The method returns the encoded string.

Parameters:

Encoding-the encoding to use, such as "UTF-8"'strict ' Ignore'replace''xmlcharrefreplace'  'backslashreplace' and any value registered through Codecs.register_error ().

Instance:

>>> str='This isa string example! '>>> str=str.encode ('utf-8','strict ' )print(str) b'This isa string example! '

Encode decodes a string encoded by string, returning the bytes type. and cannot be decoded again:

Str=str.encode ('base','strict') Traceback (most recent Call last):  "<stdin>" in <module>'  bytes'encode'

(5) Deconde ():

Decodes a string in the encoding format specified by encoding. The default encoding is string encoding.

The parameters are similar to (4):

>>> str='This is a string example!'>>> Str=str.encode ('Utf-8','Strict')>>>Print(str) b'This is a string example!'>>> Str=str.decode ('Utf-8','Strict')>>>Print(str) this isA string example!>>>Str'This is a string example!'

(6) EndsWith (suffix[, start[, end]):

The EndsWith () method is used to determine whether the string ends with the specified suffix and returns false if the end of the specified suffix returns true. The optional parameter "start" and "end" are the starting and ending positions of the retrieved string.

Parameters:

Suffix-- the argument can be a string or an element. Start  -- The starting position in the string. End    -the ending position of the character.

Returns True if the string contains the specified suffix, otherwise false.

 >>> str= "  Where does Shanghai tap water come from? Shanghai!   " >>> suffix="   Shanghai   " >>> Str.endwith (suffix,0,length (str) -1)  >>> Str.endswith (Suffix,0,len (str) -1) False  >>> Str.endswith (Suffix,0,2 >>> str[0:len (str) -1]   "  Where does Shanghai tap water come from? On   " >>> str   '  

(7) Expandtabs (tabsize=4):

turn the tab sign (' \ t ') in the string to a space, and the tab symbol (' \ t ') has a default number of spaces of 4. The method returns a new string that is generated after the tab symbol (' \ t ') in the string is converted to a space.

Print (str) hello.    MenangelPrint(str.expandtabs)Hello,       menangelprint(Str.expandtabs ( 0) Hello, menangelprint(str)   #str并未改变, this function creates a copy hello,    menangel.

(8) Find (str, beg=0, End=len (String)):

Detects if the string contains substrings of STR, and returns 1 if the specified beg (start) and end (end) ranges are checked for inclusion in the specified range, or if the containing substring returns the starting index value.

>>> str='loyalty is the foundation on which the Night\ ' s Wath was built'>>>< c3> str"Loyalty is theFoundation on which the night's Wath was built"print(str. Find ('is', 5))print(str.find ('xiaoming ', 0,len (str)-5))-1

(9) Find (str, beg=0, End=len (String))

Detects whether a substring contains substrings in str, and if you specify beg (start) and end (end) ranges, the check is included in the specified range, as in the Python Find () method, except that STR does not report an exception in string.

Parameters:

STR-----end index, which defaults to the length of the string.

Example: (The difference between find and index)

>>>Str"Loyalty is the foundation on which the night's Wath was built">>> Str.index (' is')      8>>> Str.index (' Men') Traceback (most recent): File"<stdin>", Line 1,inch<module>valueerror:substring notfound>>> Str.find (' Men')-1

Python learning process (8) string built-in function

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.