Python Learning (iii)

Source: Internet
Author: User

Chapter III Using Strings

3.1 Basic string manipulation

Sequence Operations (index, Shard, multiply, Judge membership, length, maximum, minimum) apply to a string, but the string is immutable, so the Shard assignment is illegal.

3.2 String Formatting: Lite version

% is a string formatting operator

Usage: Place a string (formatted string) on the left side and the value you want to format on the right. You can use a value (such as a string or a number), or you can use a tuple or dictionary of multiple values.

>>> Print ("%s is%s years old"% ("FSDFSD", 56))
FSDFSD is years-old

>>> format = "%s is%s years old"
>>> values = ("Robin", 34)
>>> print (format% values)
Robin is-years old

Note: Only tuples and dictionaries can format more than one value, and if you use lists or other sequences instead of tuples, the sequence will be interpreted as a value.

>>> Print ("%s is%s years old"% (["FSDFSD", 56],45))
[' FSDFSD ', ' years

Note: If you want to include a percent semicolon in a formatted string, you must use

If you are formatting real numbers (floating-point numbers), you can use F to describe the type of conversion specifier, providing precision, a period plus the number of decimal digits you want to keep:

>>> format = "PI with three decials:%.3f"
>>> from Math import pi

>>> print (format% pi)
PI with three decials:3.142

3.3 String Formatting: Full version

If the right operand is a tuple or a dictionary, each of these elements is individually formatted, and each value requires a conversion specifier.

>>> "%s%s%s"% (per-cent)
' 1 2 3 '

3.3.1 Simple Conversion

>>> "%d"% 42
' 42 '

>>> "%x"% 42
' 2a '

>>> "%f"% pi
' 3.141593 '

3.3.2 Field width and precision

Field width: The minimum number of characters retained by the converted value.

Precision: Number of decimal digits of the result

>>> "%10.2f"% pi//width is 10, accuracy is 2
' 3.14 '

3.3.3 symbol, for alignment with 0 padding

>>> '%010.2f '% pi//width is 10, filled with 0
' 0000003.14 '

>>> "%-10.2f"% pi//left Justified
' 3.14 '

>>> "% 5d"% 10//space, indicating positive number preceded by a space.
' 10 '

>>> "%+5d"% 10//+ sign, indicating whether it is a plus or minus number, displays the symbol
' +10 '

3.4 String method

3.4.1 Find the location of the substring, if not returned-1

>>> "FSDFSDFSDFDSF". Find ("FSD")
0

>>> "FSDFSDFSDFDSF". Find ("FSD", 1,5)//provide start and end points ( Note: Contains the previous index, does not contain the latter index, which is the Python idiom -1 Law ).

RFind ()/index ()/Rindex ()/count ()/StartsWith ()/EndsWith ()

3.4.2 The elements in the join sequence

The concatenated sequence must be a string

>>> "abc". Join ("de")
' DABCE '

3.4.3 Lower returns the lowercase version of the string

>>> "ABc". Lower ()
' ABC '

>>> "Sdfdfds". Capitalize ()//First Letter Capital
' Sdfdfds '

>>> "ABC". Swapcase ()//uppercase to lowercase, lowercase to uppercase
' ABC '

' FDSFSD '
>>> "ABC def ght". Title ()//capitalize the first letter of all words
' ABC Def Ght '

Upper ()//uppercase

All occurrences of the 3.4.4 replace string are replaced

>>> "This was a test". Replace ("is", "EEZ")
' Theez eez a test '

Inverse method of 3.4.5 split Join method

>>> "I am Robin". Split ("")
[' I ', ' am ', ' Robin ']

3.4.6 Strip A string that removes white space on both sides (not including interior)

>>> "FSF ASFA". Strip ()
' FSF ASFA '

3.4.7 Translate

Similar to replace, a different place is to replace only a single character

Python Learning (iii)

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.