Example analysis of string-related operations in Python

黄舟
Release: 2017-07-22 16:19:33
Original
1268 people have browsed it

This article mainly introduces the string-related operations of Python programming, and analyzes the Python string-related functions and common operation techniques in the form of examples. Friends in need can refer to it

The examples of this article describe the Python programming String related operations. Share it with everyone for your reference, the details are as follows:


#coding=utf8
'''''
字符串是Python中最常见的类型。可以通过引号见包含字符的方式创建。
Python里面单引号和双引号的作用是相同的。
字符串是不可变类型,就是说改变一个字符串的元素需要新建一个新的字符串。
字符串是由独立的字符组成,并且这些字符可以通过切片操作顺序地访问。
'''
class StringClass(object):
  '''''
  创建一个字符串就像使用一个标量一样简单。
  也可以使用str工厂函数来创建一个字符串并把它赋值给一个变量。
  '''
  #使用单引号创建字符串并赋值
  aString='signal quote'
  #使用双引号创建字符串并赋值
  anotherString="double quote"
  #使用工厂函数str把一个列表转换成一个字符串
  StringByStr=str(range(len(aString)))
  def ouput(self):
      print '''''aString:%s
anotherString:%s
StringByStr:%s''' %(self.aString,self.anotherString,self.StringByStr)
  #访问字符串的值(字符和子串)
  def getStringValue(self):
    '''''
    Python里面没有字符这个类型,而是用长度为1的字符串来表示字符这个概念。
    用方括号加一个或多于一个索引的方式来获得子串。
    '''
    #获取字符串中某个字符的值
    char=self.aString[2]
    print "aString的第三个字符是:",char
    #获取字符串从第一个到第六个个字符之间的子串
    subString=self.anotherString[0:5]
    print "anotherString第一个到第六个字符间的子串:",subString
    #获取字符串偶数为的字符
    evenSubString=self.anotherString[1::2]
    print "anotherString的偶数位字符串是:",evenSubString
  #改变字符串的值
  def changeString(self):
    '''''
    给变量赋值的方式更新一个已有的字符串。
    字符串类型是不可变的,要改变一个字符串就必须通过创建一个新串的方式来实现。
    不能只改变字符串中的一个字符或者一个子串。
    但是允许拼凑一个旧串的各个部分来得到一个新串。
    '''
    print self.aString,"aString的id:",id(self.aString),
    self.aString+=",python"
    print self.aString, "aString的id:",id(self.aString)
    #错误的操作,不允许改变字符串中的一个字符或者子串
    #self.aString[2]="h"
    #print self.aString
    #self.aString[0:2]="he"
    #print self.aString
  #删除字符和字符串
  def delString(self):
    '''''
    字符串是不可变的,不能仅仅删除一个字符串的某个字符,
    能清空一个空字符串,或者把不需要的部分通过切片操作组成一个新串。
    '''
    self.StringByStr=self.StringByStr[:3]+self.StringByStr[8:]
    print self.StringByStr
    #通过赋一个空字符串来清空一个字符串
    self.StringByStr=''
    print self.StringByStr
    #通过del删除一个字符串
    try:
      del self.StringByStr
      print self.StringByStr
    except BaseException,e:
      print e
def test():
  StrObj=StringClass()
  StrObj.ouput()
  StrObj.getStringValue()
  StrObj.changeString()
  StrObj.delString()
if __name__=="__main__":
  test()
Copy after login

Running results:

The above is the detailed content of Example analysis of string-related operations in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!