Basic Python Tutorial (second edition) Learning Note Strings (Chapter 3rd)

Source: Internet
Author: User
Tags printable characters string format

Basic Python Tutorial (second edition) Learning Note Strings (Chapter 3rd)
All basic sequence Operations (index, Shard, multiply, Judge membership, seek length, maximum minimum) also apply to strings.
The string is not to be changed;
The left side is the format string, and the right side is the value that needs to be formatted
print '%s=%d '% (' x ', ') ==> x=100
% in percent format string
Template string:
From string import Template
S=template (' $x is 100 ');  S.substitute (x= ' ABC '); ==> ' ABC is 100 '
Python's string format is very similar to the C language;
%f floating Point
%s string
%d integers
-%-10.2f left Alignment
+%+5d Positive negative numbers are marked with +-sign
Blank% 5d plus a space before positive numbers
0%010.2f left with 0 padding
Decimal%10.2f 2 decimal places
Minimum width%10.2f width of 10

String.digits Digital
String.letters Letter
String.lowercase Small Letter
string.printable printable characters
String.punctuation punctuation
String.uppercase Capital Letters

String method:
"ABCDEFG". Find ("BC") ==> 1 #查找子串; If a return subscript is found, the return-1 is not found
"ABCDEFG". Find ("DE", 2,7) # provides a range of start and end subscripts for lookups
Join connection string
X=list (' ABC '); "=". Join (x) ==> a=b=c
' ABC '. Lower () #转为小写
' ABCDE '. Replace (' AB ', ' ') ' ==> ' 12CDE ' # replacement substring
' A,b,c '. Split (', ') ==> [' A ', ' B ', ' C '] # If no broken characters are provided, the default is Space, tab, line wrap, and so on.
' ABC '. STRIP () ==> ' ABC ' # Remove the spaces at both ends
'---ABC---! '.  Strip ('-! ') ==> ' ABC ' # removes the specified characters from both ends
Translate advanced replacement, you can replace multiple substrings at the same time;

Basic Python Tutorial (second edition) Learning Note Strings (Chapter 3rd)

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.