零基础python教程:字符串
字符串是编程语言里基本的数据类型,python处理字符串方式简便。下面我来具体介绍下关于字符串的相关内容。
工具/原料
python 2.7.5
pycharm
字符串的初始化
1、a = "This is a string";b = 'This is a string';着两种声明没有任何区别,着和c的语法不同。c中的单引号代表一个字符;
字符串的截取与相加
1、a = "This is a string";b = 'This is a string';c = a[5:];d = b[1:2];print c;print d;print c + d;
字符串中匹配
1、# -*- coding: utf-8 -*-a = "Thi衡痕贤伎s is a string";if "is&鳎溻趄酃quot; in a: print "[1] is in String"else: print "is not in String"print a.find("iss"),a.find("is")
字符串格式化
1、a = "This is a string";b = 1c = 1.2312d = "%s,%d,%.2f" % (a, b, c)print d;
Unicode字符串
1、Python 中定义一个 Unicode 字符串和定义一个普通字符言射鐾钤串一样简单:a = u'th足毂忍珩is is unicode string !'print a;引号前小写的"u"表示这里创建的是一个 Unicode 字符串。
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:82
阅读量:39
阅读量:82
阅读量:28
阅读量:77