Python programming-detailed description of string-related operation examples, pythonstring

Source: Internet
Author: User

Python programming-detailed description of string-related operation examples, pythonstring

This article describes the string-related operations for Python programming. We will share this with you for your reference. The details are as follows:

# Coding = utf8''' string is the most common type in Python. You can create a bucket by using quotation marks. In Python, single quotes and double quotes have the same effect. The string type is immutable, that is, to change the element of a string, you need to create a new string. A string is composed of independent characters that can be accessed in sequence by slicing. '''Class StringClass (object): ''' creating a string is as simple as using a scalar. You can also use the str factory function to create a string and assign it to a variable. ''' # Use single quotes to create a string and assign the value aString = 'signal quote' # Use double quotes to create a string and assign the value anotherString = "double quote" # Use the factory function str to convert a list into string StringByStr = str (range (len (aString ))) def ouput (self): print ''' aString: % sanotherString: % sStringByStr: % s' % (self. aString, self. anotherString, self. stringByStr) # access string value (character and substring) def getStringValue (self): ''' Python does not have this type of character, the concept of character is represented by a string with a length of 1. Use square brackets to add one or more indexes to obtain substrings. ''' # Obtain the value of a character in the string char = self. aString [2] print "the third character of aString is:", char # obtain the subString = self between the first character and the sixth character. anotherString [0: 5] print "sub-string between the first and sixth characters of anotherString:", subString # obtain the character evenSubString = self. anotherString [1: 2] print "The even string of anotherString is:", evenSubString # change the string value def changeString (self ): ''''' update an existing string by assigning values to the variable. The string type is unchangeable. To change a string, you must create a new string. You cannot change only one character or substring in a string. However, you can splice all parts of an old string to get a new string. '''Print self. aString, "aString id:", id (self. aString), self. aString + = ", python" print self. aString, "aString id:", id (self. aString) # incorrect operation. One character or substring in the string cannot be changed # self. aString [2] = "h" # print self. aString # self. aString [0: 2] = "he" # print self. aString # delete character and string def delString (self): ''' the string is unchangeable. You cannot just delete a character from a string, but can clear an empty string, alternatively, you can use the slice operation to create a new string. '''Self. stringByStr = self. stringByStr [: 3] + self. stringByStr [8:] print self. stringByStr # Empty string self. stringByStr = ''print self. stringByStr # Use del to delete a string try: del self. stringByStr print self. stringByStr failed t BaseException, e: print edef test (): StrObj = StringClass () StrObj. ouput () StrObj. getStringValue () StrObj. changeString () StrObj. delString () if _ name __= = "_ main _": test ()

Running result:

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.