Python learning string (bottom)

Source: Internet
Author: User

----------------------------------------------
Other common string methods in real-world applications

>>>line = "The Knights who say Ni!\n"
>>>line.restrip (); Clear the end of the space
The Knights who say ni!
>>>line.upper () Case Conversion
The KNIGHTS who SAY ni!\n
The >>>line.isalpha () Isalpha () method detects whether a string consists of only letters.
False
>>>line.endswith (' ni!\n ') detects if the string ends with NI
True
>>>line.startswitch (' the ') detects if the string starts with the
True

>>>line.find (' ni ')!=-1 character detection
True
>>> ' ni ' in line
True
>>> sub = ' ni\n '
Line.endswitch (sub)
True
>>>line[-line (sub):]==sub
True

--------------------------------------------
The string format code has already been mentioned before, and we don't write an example.
String Formatting code
s string
R S but using REPR is not str
C character
D Decimal
I integer
U unsigned integer
o octal integer
X hex This man you high number
E floating-point index
F Floating Point Decimal
G floating point E or F
constant


Dictionary-based string formatting
String formatting also allows the left translation target to reference the keys in the right dictionary to extract the corresponding values

>>> '% (n) d% (x) s '%{"n": 1, "x": "Spam"}
' 1 spam '

>>>food = ' spam '
>>>age = 40
>>var ()
{' food ': ' Spam ', ' age ': 40}


>>> "% (age) d percent (food) S"%var ()
' Spam '


left-Justified and right-aligned string formatting

>>> ' {0:10} = {1:10} '. Format (' spam ', 123.4567)
' Spam = 123.4567 '
>>> ' {0:>10} = {1:<10} '. Format (' spam ', 123.4567)
' Spam = 123.4567 '
>>> ' {0[' platform ']:>10} = {1[item]:<10} '. Format ({' Platform ': ' Spam '},dict (item = 123.4567));

' Spam = 123.4567 '


The format method can also support hexadecimal octal and binary
>>> ' {0:x},{1:o},{2:b} '. Format (255,255,255)
' ff,277,11111111 '

>>> ' {0:.2f} '. Format (1/3.0)
' 0.33 '
>>> '%.2f '% (1/3.0)
' 0.33 '

Python learning string (bottom)

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.