Example of string operation method for python development, pythonstring

Source: Internet
Author: User

Example of string operation method for python development, pythonstring

This article describes the string operation method developed in python. We will share this with you for your reference. The details are as follows:

In python, we need to know about string operations, which will bring us a lot of convenience in our future development.

Below are my study notes:

# Python-string # use single quotation marks ''and double quotation marks for strings in python" to mark strA = 'this is a string 'strb = "this is a message! "# Print two strings 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 to \ n my blog slide 'print ("line feed in string") print ('stra =' + strE) print ("#############################") strF = 'this is ''' message's TrG = 'hongten 'strH = strG * 3 print ('strings can be connected with the \' + \ 'sign (or bonded), or \' * \ 'can be used to loop ') print ('strf original form: \ 'this is \ '\ 'message \ '') print ('strf:' + strF after bonding) print ('strg original value is: \ 'hongten \ ', strH = strG * 3, strH is:' + strH) print ("#############################") strI = 'hongtenzone @ foxmail.com 'print ('character string can be queried by subscript (INDEX)') print ('source string strI = \ 'hongtenzone @ foxmail.com \'') print ('string strI length, len (strI) = ') print (len (strI) print ('str I [0] = '+ strI [0]) print ('stri [10] =' + 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 values by string indexes produces an error: ') print ('stri [0] = \ 'x \', which produces an error ') print ("#############################") print ('large indexes are replaced by the string size, and the lower bound returns an empty string larger than the upper bound ') print ('stri [0: 100] =' + strI [0: 100]) print ("#############################") print ('index can be negative, count starts from the right') print ('stri [-2] = '+ strI [-2]) print ('stri [-23:] = '+ strI [-23:]) print ("#############################") print ('But-0 or 0, so it is not counted from the right! ') Print ('stri [0] =' + strI [0]) print ('stri [-0] = '+ strI [-0])

The running effect is as follows:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32Type "copyright", "credits" or "license () "for more information. >>> ================================== RESTART ======== ==========================================>>> print two strings strA = this is a stringstrB = this is a message! ########################### The Transfer Character strA in the string = I don't know anythingstrB = 'yes ', I know. ############################ line breaks in strings. strA = This is my blog, welcome to my blog ################################# strings can be used' + 'connection (or bonding ), you can also use the '*' circular strF in the original form of 'this is ''' message '. The original strF: this is messagestrG value is 'hongten ', strH = strG * 3. In this case, strH is: hongtenHongtenHongten ############################# subscript (INDEX) can be used for strings) query the source string strI = 'hongtenzone @ foxmail. c Om 'string strI length, len (strI) = 23 strI [0] = hstrI [10] = estrI [-1] = strI [len (strI) -1] strI [-1] = mstrI [len (strI) -1] = m ############################# the Python string cannot be rewritten. An error occurs when assigning values by string index: strI [0] = 'x ', in this case, an error occurs ############################# replace an excessively large index string size, returns an empty string strI [0: 100] = hongtenzone@foxmail.com ########################## ### indexes can be negative, count from right start strI [-2] = ostrI [-23:] = hongtenzone@foxmail.com ############################# but-0 or 0, so it does not count from the right! StrI [0] = hstrI [-0] = h >>>

The print string statement is as follows:

Print ('the best way to understand slice is to regard the index as a point between two characters. The left side of the first character is 0, and the right side of the n character in the string 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 each index from 0 to 5 in the string, the second row is the corresponding negative index. The slice from I to j is composed of characters between the two flags ') print (' for non-negative indexes, the slice length is the difference between the two indexes. For example, the length of word [] is 2 ')

The running effect is as follows:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32Type "copyright", "credits" or "license () "for more information. >>> ================================== RESTART ======== ======================================>> the best way to understand the slice is to regard the index as between two characters point, the left side of the first character is 0, the right side of the n character in the string is the 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 each index from 0 to 5 in the string, the second line is Negative index. The slice from I to j is composed of characters between the two flags. For non-negative indexes, the slice length is the difference between the two indexes. For example, the length of word [] is 2 >>>

I hope this article will help you with Python programming.

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.