Python3 manual-Basic Data Type-string

Source: Internet
Author: User
Tags iterable printable characters
Judgement-A bool value is usually returned.
Str. isalpha () Whether to include only text
Str. isdecimal () Whether to include only numbers (multilingual numbers)
Str. isdigit () Whether to include only numbers (0 ~ 9)
Str. isnumeric () Whether to contain only numeric characters
Str. isalnum () Whether it only contains text and numbers
Str. isidentifier () Is it a legal identifier?
Str. islower () Lowercase?
Str. isupper () Are all uppercase letters?
Str. istitle () Whether the first letter of each word is capitalized
Str. isprintable () Whether to include only printable characters
Str. isspace () Whether to contain only white space characters
Str. startswith (prefix [,
Start [, end])
Prefix or not
Str. endswith (suffix [,
Start [, end])
End with suffix?
Modifier-a modified string is usually returned.
Str. capitalize () Returns an uppercase string.
Str. Title () Returns an uppercase string of each word.
Str. expandtabs ([tabsize]) "\ T" to convert to Space
Str. Upper () Full conversion to uppercase
Str. Lower () Convert all to lowercase letters
Str. Ljust (width [,
Fillchar])
Align left and fill right
Str. Must ust (width [,
Fillchar])
Right alignment, left Filling
Str. Center (width [,
Fillchar])
Center, filled on both sides
Str. lstrip ([chars]) Remove left white space or custom characters
Str. rstrip ([chars]) Remove right white space or custom characters
Str. Strip ([chars]) Remove white spaces or custom characters on both sides
Str. swapcase () Case sensitivity
Str. zfill (width) Fill the left side with 0 to the specified width, which is generally used to modify numbers
Search & replace
Str. Count (sub [,
Start [, end])
Calculate the number of sub occurrences between [start, end)
Str. Find (sub [, start [, end])  
Str. Index (sub [, start [, end])  
Str. rfind (sub [, start [, end])  
Str. rindex (sub [, start [, end])  
Str. Replace (old, new [, Count])  
Splitting & Combination
Str. Join (iterable)  
Str. Partition (SEP)  
Str. rpartition (SEP)  
Str. Split ([Sep [, maxsplit])  
Str. rsplit ([Sep [, maxsplit])  
Str. splitlines ([keepends])  
Conversion
Hex (X)  
INT ([number | string [, base])  
Len (s)  
List ([iterable])  
Oct (X)  
Ord (c)  
Repr (object)  
Reversed (SEQ)  
STR ([object [, encoding [, errors])  
Upper top priority Str. isalpha ()
-Whether to include only text
Code Result
Print ("China ABC". isalpha ()) True
Print ("". isalpha ()) False
Print ("123". isalpha ()) False
Print ("". isalpha ()) False
Maximum top priority Str. isdecimal ()
-Whether to include only decimal numbers, including multilingual numbers
Code Result
Prints ("1234567890". isdecimal ()) True
Print ("\ u0660". isdecimal ()) True
Print ("ABC". isdecimal ()) False
Print ("". isdecimal ()) False

For numbers in other languages see http://www.fileformat.info/info/unicode/category/Nd/list.htm

Upper top priority Str. isdigit ()
-Whether to include only numbers (0 ~ 9)
Code Result
Print ("1234567890". isdigit ()) True
Print ("\ u0660". isdigit ()) True
Print ("ABC". isdigit ()) False
Print ("". isdigit ()) False
Maximum top priority Str. isnumeric ()
-Whether to include only numeric characters
Code Result
Print ("1234567890". isnumeric ()) True
Print ("\ u2155". isnumeric ()) True
Print ("ABC". isnumeric ()) False
Print ("". isnumeric ()) False

For numeric characters, see http://www.fileformat.info/info/unicode/category/No/list.htm

Maximum top priority Str. isalnum ()
-Whether to include only text and numbers
Code Result
Print ("abc123456 \ u2155". isalnum ()) True
Print ("". isalnum ()) False
Print ("\ t". isalnum ()) False
Print ("". isalnum ()) False
Maximum top priority Str. isidentifier ()
-Is it a legal identifier?
Code Result
Print ("if". isidentifier ()) True
Print ("China". isidentifier ()) True
Print ("123". isidentifier ()) False
Print ("". isidentifier ()) False
Comment top comment Str. islower ()
-Whether it is in lower case
Code Result
Print ("ABC". islower ()) True
Print ("ABC". islower ()) False
Print ("China". islower ()) False
Print ("". islower ()) False
Upper top priority Str. isupper ()
-Are all uppercase letters?
Code Result
Print ("Hello world". istitle ()) True
Print ("Hello world". istitle ()) False
Print ("Hello world". istitle ()) False
Print ("". istitle ()) False
Upper top priority Str. istitle ()
-Whether the first letter of each word is capitalized
Code Result
Print ("Hello world". istitle ()) True
Print ("Hello world". istitle ()) False
Print ("Hello world". istitle ()) False
Print ("". istitle ()) False
Wrote top response Str. isprintable ()
-Whether to include only printable characters
Code Result
Print ("a B". isprintable ()) True
Print ("". isprintable ()) True
Print ("ABC \ t". isprintable ()) False
Print ("ABC \ n". isprintable ()) False
Maximum top priority Str. isspace ()
-Whether to include only white space characters
Code Result
Print ("". isspace ()) True
Print ("\ t \ n". isspace ()) True
Print ("a B". isspace ()) False
Print ("". isspace ()) False
Upper top priority Str. startswith (prefix [,
Start [, end])-whether to start with prefix
Code Result
Print ("Chinese". startswith ("medium ")) True
Print ("Chinese". startswith ("China", "I "))) True
Wrote top response Str. endswith (suffix [,
Start [, end])-whether to end with suffix
Code Result
Print ("Chinese". endswith ("person ")) True
Print ("Chinese". endswith ("Chinese", "I "))) True
Upper top priority Str. capitalize ()
-Returns an uppercase string.
Code Print ("the first sentence. The second sentence.". capitalize ())
Result The first sentence. The second sentence.
Top priority Str. Title ()
-Returns an uppercase string of each letter.
Code Print ("this is a title". Title ())
Result This is a title
Using Top response Str. expandtabs ([tabsize])
-"\ T" to convert to Space
Code "\ T". expandtabs (8)
Result ''
Upper top priority Str. Upper ()
-Full conversion to uppercase
Code Print ("ABC". Upper ())
Result ABC
Comment top comment Str. Lower ()
-Convert all data to lowercase letters.
Code Print ("ABC". Upper ())
Result ABC
Upper top priority Str. Ljust (width [,
Fillchar])-align left and fill right
Code Print ("I". Ljust (4, ""))
Result

We

Upper top priority Str. Must ust (width [,
Fillchar])-Right alignment, left Filling
Code Print ("I". Allowed ust (4, "= "))
Result

=== Me

Upper top priority Str. Center (width [,
Fillchar])-center, filled on both sides
Code Print ("I Am a split line". Center (30, "= "))
Result ================ I am a split line ======================
Upper top priority Str. lstrip ([chars])
-Remove left white space or custom characters
Code 'Spacious '. lstrip ()
Result 'Spacious'
Code 'Www .example.com '. lstrip ('cmowz .')
Result 'Example. com'
Upper top priority Str. rstrip ([chars])
-Remove right white spaces or custom characters
Code 'Spacious '. rstrip ()
Result 'Spacious'
Code 'Mississippi '. rstrip ('ipz ')
Result 'Mississ'
Upper top priority Str. Strip ([chars])
-Remove blank spaces or custom characters on both sides
Code 'Spacious '. Strip ()
Result 'Spacious'
Code 'Www .example.com '. Strip ('cmowz .')
Result 'Example'
Using Top response Str. swapcase ()
-Case-insensitive Conversion
Code Print ("ABC". swapcase ())
Result ABC
Upper top response Str. zfill (width)
-Fill the left side with 0 to the specified width, which is generally used to modify numbers.
Code Print ("15". zfill (8 ))
Result 00000015
Code Print ("-15". zfill (8 ))
Result -0000015
Running Top response Str. Count (sub [,
Start [, end])-calculates the number of sub occurrences between [start, end ).
Code Print ("abababab". Count ("Abab ")
Result 2

Note: non-overlapping count, so the result is 2 instead of 3

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.