Python-Developed string manipulation method examples

Source: Internet
Author: User
The examples in this article describe the string manipulation method of Python development. Share to everyone for your reference, as follows:

In Python, it is necessary to understand the operation of string strings, which will bring us a lot of convenience in our future development.

Here are the notes I studied:

The strings in the #python-string#python are marked with a single quote "" and a double quote "" Stra = ' This is a string ' StrB = "The Is a message!" #打印两个字符串print ("Print Two Strings") print (' Stra = ' + stra) print (' StrB = ' + StrB) print ("#############################") strc = ' I don \ ' t know anything ' StrD = ' \ ' yes\ ', I know. ' Print ("escape character in String") print (' Stra = ' + strc) print (' StrB = ' + StrD) print ("######### #################### ") Stre = ' This is my blog, welcome everyone to \ \ \ \ \ \" "Print (" line wrap in string ") print (' Stra = ' + stre) print (" ################# ############ ") strf = ' This is ' message ' StrG = ' hongten ' STRH = StrG * 3print (' string can be connected (or bonded) with \ ' +\ '), or it can be used with the ' *\ ' Loop ') print ( ' strf original form: \ ' This is \ ' \ ' message\ ') print (' Bonded strf: ' + strf ') print (' StrG original value: \ ' hongten\ ', STRH = StrG * 3, at this time STRH is: ' + STRH Print ("#############################") Stri = ' hongtenzone@foxmail.com ' Print (' string can use subscript (indexed) query ') print (' source string stri = \ ' Hongtenzone@foxmail.com\ ') print (' string stri length, len (stri) = ') print (len (stri)) print (' stri[0] = ' + stri[0]) print (' Stri [Ten] = ' + stri[10]) print (' stri[-1] = Stri[len (stri)-1] ') print (' stri[-1] = ' + stri[-1]) print (' Stri[len (stri)-1] = ' + Stri[len (stri)-1]) print (' ############################# ') print (' Python string cannot be rewritten. Assigning a value by a string index produces an error: ') print (' stri[0] = \ ' x\ ', which produces an error ') Print ("#############################") print (' oversized index instead of a string size, The lower bound returns an empty string ') print (' stri[0:100] = ' + stri[0:100]) print (' ############################# ') print (' Index can be negative, counting starts from the right) Print (' stri[-2] = ' + stri[-2]) print (' stri[-23:] = ' + stri[-23:]) print ("#############################") print (' but-0 or 0 , so it's not counted from the right! ') print (' stri[0] = ' + stri[0]) print (' stri[-0] = ' + stri[-0])

The results are as follows:

Python 2.7.9 (default, Dec, 12:24:55) [MSC v.1500 + bit (Intel)] on Win32type "copyright", "credits" or "license ( ) "For more information.>>> ================================ RESTART ================================> >> Print two strings Stra = this is a stringstrb = This is a message!############################ #字符串中的转移字符strA = I don ' t know ANYTHINGSTRB = ' Yes ', I know.############################ #字符串中的换行strA = This is my blog, welcome everyone to my blogger stroll ######################## # # # # # # #字符串可以用 ' + ' connection (or glue), can also use the ' * ' Loop strf the original form: ' This is ' message ' after the Strf:this is MESSAGESTRG value is: ' Hongten ', STRH = StrG * 3, at this time strh: hongtenhongtenhongten############################ #字符串可以使用下标 (Index) query source string stri = ' Hongtenzone@foxmail.com ' string stri length, len (stri) = 23stri[0] = hstri[10] = estri[-1] = Stri[len (stri)-1]stri[-1] = Mstri[len (Stri)-1] = m############################ #Python string cannot be overwritten. Assigning a value by a string index produces an error: stri[0] = ' x ', which results in an error ############################ #过大的索引代替为字符串大小, the lower bound returns an empty string stri[0:100] = hongtenzone@foxmail.com############################ #索引可以是负数, counting starts from the right stri[-2] = ostri[-23:] = hongtenzone@foxmail.com############################# However-0 or 0, so it is not counted from the right! Stri[0] = hstri[-0] = h>>>

Print prints the string statement as follows:

Print (The best way to understand slices is to think of the index as a point between two characters, the first character to the left is 0, the nth character in the string to the right is Index n ') print (' +---+---+---+---+---+ ') print (' | H | e | l | P | A | ') Print (' +---+---+---+---+---+ ') print (' 0  1  2  3  4  5 ') print (' -5-4 -3-2 -1-0 ') print (' The first line is the position of the given 0 to 5 indices in the string, and the second row is the corresponding negative index. The slice from I to J consists of the characters between these two flags ') print (' for nonnegative indexes, the slice length is the difference between the two indexes. For example, the length of Word[1:3] is 2 ')

The results are as follows:

Python 2.7.9 (default, Dec, 12:24:55) [MSC v.1500 + bit (Intel)] on Win32type "copyright", "credits" or "license ( ) "For more information.>>> ================================ RESTART ================================> >> the best way to understand slices is to think of the index as a point between two characters, the left side of the first character is 0, the right side of the nth character in the string is index n +---+---+---+---+---+  | H | e | l | P | A | +---+---+---+---+---+  0  1  2  3  4  5-5-4-3-2-1-0 The first line is the position of the given 0 to 5 indexes in the string, and the second row is the corresponding negative index. Slices from I to j consist of the characters between these two flags for nonnegative indexes, and the slice length is the difference between the two indexes. For example, the length of Word[1:3] is 2>>>

I hope this article is helpful for Python program design.

  • 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.