Python string manipulation

Source: Internet
Author: User

Today, the problem of resolving the software name and version number from the URL is recorded here, mainly Python string manipulation and common regular expressions.

The string in Python is an immutable sequence, meaning that a defined string cannot be modified in place.

    • Python string declaration
' Hello World '  "helloWorld", "Hello  World"  "" "HelloWorld" ""

The declaration of a python string can be a single or double quotation mark, or it can be a three-quote, single, double-quoted, but cannot be mixed.

If you mix Python's single and double quotes, you need to add the escape character \

" he\ ' s Mom "
    • Raw string Suppression escape

We have something to do with the file system, if it's a novice, it might.

MyFile = open ('C:\new\text.dat','w')

But this can be problematic because \ nthe is escaped, so to avoid this, we can use the following two ways:

MyFile = open ('c:\\new\\text.dat','w'= open ( R'C:\new\text.dat','w')

The second way is recommended, because there are no more slashes to confuse.

    • Methods used to process strings in real-world projects
STR6 ="goodevening"Printlen (str) STR7= str1 +str2PrintSTR7Print "hi!"* 4#Traversing Strings forChinch "Hello World":    PrintCh.upper ()#capitalize a string#indexes and ShardsBCD ="ABCDEFG"[1:4]#BCDEFG ="ABCDEFG"[-3:]#EFGc ="ABCDEFG"[2]#determines whether a string is a number"123". IsDigit ()#True"123.22". IsDigit ()#False#String Formatted outputinfo ="My name is {0} I am {1} years old". Format ("MJ","Wuyi")PrintInfo#String Lookup"Hello World". Find ("Lo")#3"Hello World". Find ("o", 6, 8)#7"Hello World". Find ("Morning")#-1#string Substitution"Hello". replace ("Hi","Ye")#Hello because I can't find Hi"Hello". replace ("H","h")#Hello#Remove SpacesPrintLen"Strip")#Length of 7PrintLen"Strip". Strip ())#Length of 5#determine whether to end with a string beginningPrint "StartEnd". StartsWith ("Start")Print "StartEnd". EndsWith ("End")#string transcodingPrintOrd"a")# thePrintChr (98)#b

Let's just write it up and have time to add it.

  

Python string manipulation

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.