Exemple d'analyse d'opérations liées aux chaînes en Python

黄舟
Libérer: 2017-07-22 16:19:33
original
1268 Les gens l'ont consulté

Cet article présente principalement les opérations liées aux chaînes de la programmation Python et analyse les fonctions liées aux chaînes Python et les techniques de fonctionnement courantes sous forme d'exemples. Les amis dans le besoin peuvent s'y référer

Les exemples de ceci. L'article raconte l'histoire de la programmation Python. Partagez-le avec tout le monde pour votre référence, les détails sont les suivants :


#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()
Copier après la connexion

Résultats en cours :

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!