. NET Programmer's Python basic tutorial Learn----the use of strings [Second Day]

Source: Internet
Author: User
Tags print format string format

Learn the use of tuples in the FirstDay, and start learning the use of strings today. The use of strings mainly to master, the format of the string (C language We should all know, Python and C language is not very different), the basic operation of the string (Join,split,replace, etc. and. NET also have a lot of similarities)

 I. Basic operation of STRINGS

1. String Formatting awareness : The formatting of the string is separated from the C language in%. The assignment is also used in% (VAL1,VAL2) to pass the formatted data in the form of tuples.

The number of Note:values elements and the format must correspond to one by one of the formatted string. Otherwise you will get an error.

' Hello.%s%s '>>> values = ('World','Frank') >>> Print format%Valueshello. World Frank

2. String format conversion type : String formatting is divided into many types, the basic usage type is as follows:

Conversion type Meaning
D Signed Decimal
O Octal with no symbols
U Non-Signed decimal
X (lowercase) Non-signed 16 binary
X (uppercase) Signed 16-binary
F Floating point number
C Single character
S String

3. Conversion of strings:

%[format width [Escape type]: Sets the length after formatting. More out of the ignore

%. Reserved decimal places [numeric type]: Sets the number of digits to retain after the decimal point

%[fill character [format width] [Transfer Type]: This method is more, it is recommended that you test yourself. Different types of different situations result in inconsistent results. I don't have a uniform list of things here.

>>>'The float is%.2f'% (1.0/3)#2-bit valid decimal point reserved'The float is 0.33'>>>'Your Name is%10s'%'Frank' #set width to ten'Your Name is Frank'>>>'%+10d'%#set width and front sky + number'+1'>>>'%010d'%#set width front with 0 complement'0000000001'

4. String Template : In addition to string formatting, Python also provides another format method, called string template, mainly through the template of the substitute method to try to support the operation of the dictionary.

Note: string templates can be replaced directly by a dictionary, and the principle of substitution is achieved through mapping (mapping). Call substitute on the whole. The $ field inside the temlate must exist in the key of the dictionary, otherwise the error message indicates that mapping does not exist, but can be avoided by Safe_substitute. Take a look at the following code:

>>> fromStringImportTemplate>>> dicmsg = template{'Name1':'Frank','Name2':'Loch'}>>> s = Template ('$Name 1 and $Name 2 are Ecoworks')>>>S.substitute (dicmsg)'Frank and Loch are Ecoworks'>>> dicmsg ={'Name1':'Frank'}>>>S.safe_substitute (dicmsg)'Frank and $Name 2 is Ecoworks

>>> dicmsg ={' Name1 ': ' Frank ', ' Name2 ': ' Loch ', ' Name3 ': ' Hardy '}
>>> S.substitute (dicmsg)
' Frank and Loch are Ecoworks '

  

Two. String common methods.

Like. NET and Java, Pyhon provides a number of ways to manipulate strings. such as Upper,find,replace, etc.

1.Find: Find out the first match to the subscript of a string. If there is no return-1

    

 >>>  " frank Zhang   ' . Find ( " zhang   '   '  6>>>  '    

2.Join: The list is linked by a specific character, remember that the list must be a list of strings, reasoning is connected by the list traversal and then the string to add, if it is an integer, then in the execution want to add error.

    

>>> list1=[1,2,3]>>>'-'. Join (List1) Traceback (most recent call last): File"<pyshell#32>", Line 1,inch<module>'-'. Join (LIST1) typeerror:sequence item 0:expected string, int found>>> list2=['1','2','3']>>>'-'. Join (LIST2)>>> '--'. Join ([' 1 ', ' 2 ', ' 3 ')
' 1--2--3 '

3.Split and Splitline: A string split that splits a string into a list by a specific character.

    

>>>'1-2-3-4-5'. Split ('3-4')['1-2-','-5']>>>'1-2-3-4-5'. Split ('-')['1','2','3','4','5']>>>'1-2-3-4-5'. Split (' -')['1-2','4-5']>>>'123\n456'. Splitlines () ['123','456']>>>'123\n456'. Split ('\ n')['123','456']

4. Character Case conversion: Python provides a number of useful string case conversions, including all conversions, where the first letter of the word is converted to uppercase.

    

>>>'Frank'. Upper ()#Convert to uppercase'FRANK'>>>'Frank'. Lower ()'Frank'>>>'FRANK'. Islower () False>>>'Frank'. Islower () True>>>'My name is Frank'. Title ()#capitalize the first letter of each word'My Name is Frank'>>>'Aaddee'. Swapcase ()#convert each letter case to another'Aaddee'>>>'This is python'. Capitalize ()'This is python

5.strip: Kick out the space symbol before and after the string, similar to the. NET Trim () method.

 ". Strip () ' Frank '  . Rstrip ()'  Frank'. Lstrip () '  '

Three. Summary:

    

In     general, Python's string manipulation is quite simple. There are also some basic methods of invocation. The formatting string differs little from the C language. String operations with. NET are not too large         . It's time for lunch. Afternoon and colleagues go skating rink skating, the second skating, today to turn    ' and ' brake ' learn, PS It's good to go to work from Tuesday to Saturday. Going to the skating rink in Monday was a special session for us. No one else, for beginners to practice. Ha ha. Looking forward to a wonderful afternoon.


    

. NET Programmer's Python basic tutorial Learn----the use of strings [Second Day]

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.