Getting started with python-strings

Source: Internet
Author: User

String

A string is any text enclosed in single or double quotation marks " , such as ‘abc‘ , and "123" so on.

Note that ‘‘ or "" itself is just a representation, not part of a string, so the string is ‘abc‘ only a , b c this 3 characters. If it is also a character, it can be "" enclosed, such as the character that contains it,,, the space, the "I‘m OK" I m O K 6 characters.

To create a string:

1 a = 'Hello world! ' 2 B = "Python ralvin"

Corresponding operation:

1 #1 * Repeating output string2 Print('Hello'* *)3  4 #2 [], [:] Get the character in the string by index, here and the list of slice operation is the same, the concrete content see list5 Print('HelloWorld'[2:])6  7 #3 in member operator-returns TRUE if the string contains the given character8 Print('El' inch 'Hello')9  Ten #4% format string One Print('Alex is a good teacher') A Print('%s is a good teacher'%'Alex') -   -   the #5 + string concatenation -A='123' -b='ABC' -C='789' +d1=a+b+C - Print(D1) + #+ low efficiency, the use of join AD2="'. Join ([a,b,c]) at Print(D2)

Python's built-in approach:

1 #string.capitalize () capitalizes the first character of a string2 #string.center (width) returns the center of the original string and fills the new string with a space of length width3 #string.count (str, beg=0, End=len (String)) returns the number of occurrences of STR in a string, if beg or end specifies the number of occurrences of STR in the specified range4 #String.decode (encoding= ' UTF-8 ', errors= ' strict ') decodes a string in encoding specified encoding format, if an error defaults to a ValueError exception unless err ORS specifies ' ignore ' or ' replace '5 #String.encode (encoding= ' UTF-8 ', errors= ' strict ') encodes a string in encoding specified encoding format, and if an error defaults to a ValueError exception, unless errors specifies Is ' ignore ' or ' replace '6 #string.endswith (obj, beg=0, End=len (string)) checks whether the string ends with obj, if beg or end specifies whether the specified range ends with obj, if yes, returns TRUE, no False is returned.7 #string.expandtabs (tabsize=8) Turns the tab symbol in string strings to a space, and the default number of spaces in the tab symbol is 8. 8 #string.find (str, beg=0, End=len (string)) detects if STR is contained in a string, and if beg and end specify a range, then the check is contained within the specified range if it is the index at which the start is returned Value, otherwise return-19 #string.index (str, beg=0, End=len (String)) is the same as the Find () method, except that STR does not report an exception if it is not in a string.Ten #String.isalnum () returns True if the string has at least one character and all characters are letters or numbers, otherwise False One #String.isalpha () returns True if the string has at least one character and all characters are letters, otherwise False A #String.isdecimal () returns True if the string contains only a decimal number, otherwise False is returned. - #string.isdigit () returns True if the string contains only a number, otherwise False. - #String.islower () returns True if the string contains at least one case-sensitive character, and all of these (case-sensitive) characters are lowercase, otherwise Fal Se the #string.isnumeric () returns True if the string contains only numeric characters, otherwise False - #string.isspace () returns True if the string contains only spaces, otherwise False. - #String.istitle () returns True if string is heading (see Title ()), otherwise False - #String.isupper () returns True if the string contains at least one case-sensitive character, and all of these (case-sensitive) characters are uppercase, otherwise Fal Se + #string.join (seq) merges all the elements in the SEQ (the string representation) into a new string, using string as a delimiter - #string.ljust (width) returns the left alignment of an original string and fills the new string with a space of length width + #String.Lower () converts all uppercase characters in a string to lowercase. A #String.lstrip () truncates the left space of a string at #String.maketrans (Intab, Outtab]) the Maketrans () method is used to create a conversion table of character mappings, for the simplest invocation of two parameters, the first argument is a string representing the character that needs to be converted , the second parameter is also the target of the string representation transformation.  - #Max (str) returns the largest letter in the string str.  - #min (str) returns the smallest letter in the string str.  - #string.partition (str) is a bit like the combination of find () and split (), which, starting from the first position in Str, divides the string into a 3 The tuple of elements (STRING_PRE_STR,STR,STRING_POST_STR), if the string does not contain str, then string_pre_str = = String. - #string.replace (str1, str2, Num=string.count (STR1)) replaces str1 in string with STR2, and if NUM is specified, the substitution is not more than num times. - #string.rfind (str, Beg=0,end=len (string)) is similar to the Find () function, but looks up from the right. in #string.rindex (str, Beg=0,end=len (string)) is similar to index (), but starts from the right. - #string.rjust (width) returns the right alignment of the original string and fills the new string with a space of length width to #string.rpartition (str) is similar to the partition () function, but looks up from the right. + #String.rstrip () deletes a space at the end of a string string. - #String.Split (str= "", Num=string.count (str)) slices a string with the Str delimiter, and if NUM has a specified value, only the NUM substring is delimited the #string.splitlines (num=string.count (' \ n ')) is separated by rows, returns a list containing the rows as elements, and if num specifies only a slice of num rows. * #string.startswith (obj, Beg=0,end=len (string)) checks whether the string starts with obj, returns True, or False. If beg and end specify a value, the check is within the specified range. $ #String.strip ([obj]) executes Lstrip () and Rstrip () on stringPanax Notoginseng #string.swapcase () flips the casing in a string - #String.title () returns a string that is "heading", meaning that all words start with uppercase and the remaining letters are lowercase (see istitle ()) the #string.translate (str, del= "") converts string characters according to the table given by STR (contains 256 characters), and the characters to be filtered out into the del parameter + #string.upper () converts lowercase letters in string to uppercase

Getting started with python-strings

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.