Python lesson Fourth (string of sequences)

Source: Internet
Author: User

    • In Python, the use of strings is ubiquitous, and methods that can be called by a string are the most common in comparison to lists and tuples. The string is also one of the 6 built-in sequences in Python.
    • Basic manipulation of strings

As a sequence, the general operation of the sequence (index, shard, addition, multiplication, membership, length, maximum, minimum) is applicable for the string. However, strings and tuples are immutable, so it is illegal to assign values to a string.

    • formatting strings

What is a formatted string? The operation of a string that has a specific format that can replace a tagged position in a particular format by a given value is called a formatted string. For example, "%s year%s month%s Day" does not change the character portion of the string, only give 3 numbers sequentially instead of the "%s" in the string, the location of the tag, to achieve the dynamic date and month of the output.

Why use a formatted string? As with functions in Python, for frequently used cases, avoid frequently repeated input fixed code for strings that are complex in format.

How to use: "% conversion flag/field width." Precision/conversion type "% value

"%": the start of the token conversion specifier

Conversion flag: "-" for left-aligned, "+" for the number of positive signs before the real numbers; "" (white space character) to preserve spaces before integers; "0" indicates that the number of digits is not sufficient when the character "0" is filled;

Field width: The converted string must have at least the width of the value. A value of "*" indicates that the width is read from a tuple that follows

Point. Precision: If the conversion is real, the number of digits after the decimal point, or if the string is converted, the maximum field width of the converted string is represented. If "*" indicates that the precision value is read from the tuple.

Conversion type: s (string) for character type, d for integral type, F for floating point, and maximum for these three character types

1. Simple conversion

1 >>> msg='**********info of%s**********2... name:%s3  ... age:%d'4print(msg% ('lufei', ' Lufei ', +))5 **********info of lufei**********6Name:lufei7 age:19

2. Field width

1 ' The price of aggs:%d '%12     2'the price ofaggs:12'3'the price of aggs:%10d'%12   #字段宽度4'the price ofAggs:         

3. Conversion flag

1>>>'The price of aggs:%10d'%12 #字符宽度 102 'The price of aggs:12'3>>>'The price of aggs:%-10d'%12 #字符宽度 10 Align Left4 'The price of aggs:12'5>>>'The price of aggs:%+10d'%12 #字符宽度 10 Displays the positive and negative sign of the value6 'The price of Aggs: +12'7>>>'The price of aggs:%+10d'%-12 #字符宽度 10 Displays the positive and negative sign of the value8 'The price of Aggs: -12'9>>>'The price of aggs:%010d'%12 #字符宽度 10-bit number is not enough to use 0 paddingTen 'The price of aggs:0000000012'

4. Accuracy

1>>>'The price of aggs:%d'%122 'The price of aggs:12'3>>>'The price of aggs:%.5d'%12 #转换的是整数, indicating the maximum width of the field, less than 0 padding for the number of bits4 'The price of aggs:00012'5>>>'Login name:%s'%'Operadmin'6 'Login Name:operadmin'7>>>'Login Name:%.5s'%'Operadmin' #转换的是字符串, which indicates the maximum width of the field, which is discarded beyond the maximum width8 'Login Name:opera'
9 >>> ' The price of aggs:%.5f '%12 #转换的是实数, indicating the number of digits after the decimal point
Ten ' The price of aggs:12.00000
    • String method
1 1str.capitalize () Capitalize first letter2 2str.casefold () uppercase in the string is replaced by lowercase3 3str.center (Width,filechar) string Center print4 4str.count (X,start,end) counts the number of occurrences of a substring in a string5 5str.encode (self,encoding,errors)6 6str.endswith (self,suffix,start,end) determines whether a string ends with a substring7 7str.expandtabs (tabsize)88str.find (sub,start,end) returns the index of a character or substring in a string. If the substring or character does not exist return-19 formatted output of the 9str.format () stringTen10str.index (sub,start,end) returns the index of a character or substring in a string. If the substring or character does not present an error "substring notfound " One 11str.isalnum () determines whether a string contains only letters and numbers A 12str.isalpha () determines whether a string contains only alphabetic characters - 13str.isdecimal () determines whether a string contains only numbers - 14str.isdigit () determines whether a string contains only numbers the 15str.identifier () to determine if a string is a standard variable name - 16str.islower () determines whether the grandmother characters in a string are lowercase characters - 17str.isnumeric () determines whether a string contains only numbers - 18str.isprintable () determines whether a string is a printable string + 19str.isspace () to determine if a string contains only spaces - 20str.istitle () determines whether a string is a title (the first letter of the word is lowercase in other letters) + 21str.isupper () determines whether alphabetic characters in a string are uppercase A 22str.join (sequence) inserts STR between each of the two elements in a sequence at 23str.ljust (Width,fillchar) string left-aligned, not wide enough to use character padding, by default with space padding - 24str.lower () converts uppercase letters in a string to lowercase letters - 25str.lstrip (char) removes the specified character at the beginning of the string, and the default is a space - 26maketrans - 27str.partition (str) splits the string according to the specified delimiter. If the string contains delimiters, the chapter is a tuple of $3 - 28str.replace (Old,new,count) replaces a character or substring in the original string with the specified string, and can specify a replacement quantity in29str.rfind (sub,start,end) returns the index of a character or substring in a string, starting from the right. Returns "1 if the substring or character does not exist"" -30str.rindex (sub,start,end) returns the index of a character or substring in a string. Query from right, if substring or character does not exist error "substring notfound " to 31str.rjust (Width,fillchar) function In contrast to Ljust, one is left aligned and one is right justified + 32str.rpartition (str) splits the string with the specified delimiter, starting from the right to query the delimiter as the split point. Returns a tuple of $3 - 33str.rsplit (Str,num) finds the specified character as a split-point split string, finds it from the right, and optionally splits a num substring, returning a list of split substrings the 34str.rstrip (char) removes the specified character at the end of the string, and the default is a space * 35str.split (Char,num) finds the specified character as the split-point split string, specifying the split-to-NUM substring, which returns a list of the segmented substrings $ 36str.splitlines () split by line ' \ n ' \ r \ n ' if the argument keepends true preserves the newline character, and if Keepends is false, the newline character is not preserved .Panax Notoginseng 37str.startswith (char,start,end) determines whether a string starts with a character and can specify the starting and ending positions - 38str.strip (char) removes the specified character at the beginning and end of the string, with a space by default the 39swapcase + 40str.upper () converts lowercase letters in a string to uppercase letters A41str.zfill (width) Specifies the width of the string, the width is not enough, the string is right-aligned using the character "0" padding

Python lesson Fourth (string of sequences)

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.