1、字符串

1.1索引和切片

索引:

>>> lang = "study python"
>>> lang[0]
's'
>>> lang[1]
't'
>>> "study python"[0]
's'

字符串中对应的索引:

通过字符串找索引:

>>> lang.index("p")
6

字符串切片:

>>> lang = "study python"
>>> lang[2:9]
'udy pyt'
>>> lang = 'study python'
>>> b = lang[1:] #得到从1号到末尾的字符,这时最后那个序号不用写
>>> b
'tudy python'
>>> c = lang[:] #得到所有的字符
>>> c
'study python'
>>> d = lang[:10] #得到从第一个到10号之前的字符
>>> d
'study pyth'
>>> e = lang[0:10]
>>> e
'study pyth'
>>> lang[1:11] #如果冒号后面有数字,所得到的切片不包含数字所对应的序号(前包括,后不包括)
'tudy pytho'
>>> lang[1:]
'tudy python'
>>> lang[1:12]
'tudy python'
>>> lang[1:13]
'tudy python'

1.2字符串基本操作

len():求序列长度   #返回值为一个整数

+:连接2个序列

*:重复序列元素

in:判断元素是否存在于序列中

max():返回最大值

min():返回最小值

+:

>>> str1 = 'python'
>>> str2 = 'lisp'
>>> str1 + str2 #字符串连接
'pythonlisp'
>>> str1 + "&" + str2
'python&lisp'

in:

>>> str1 = "python"
>>> str2 = "lisp"
>>> "p" in str1 #判断某个字符传是不是在另外一个字符串内,包含返回True 返回False
True
>>> "th" in str1
True
>>> "l" in str2
True
>>> "l" in str1
False

max、min、ord、chr:

>>> max(str1)   #最值比较,按照ASCLL码
'y'
>>> min(str1)
'h'
>>> ord("y")    #查看ASCLL码对应的顺序
121
>>> ord("h")
104
>>> chr(104) #通过ASCLL码顺对应顺序查找字符
'h'

字符串比较

>>> 'a' > 'b'
False
>>> 'a' < 'b'
True
>>> "abc" > "aaa" #按照顺序比较字符串 1如果相等对比2,直到对比出大小
True
>>> "abc" < "a c"
Fals

重复字符

>>> a * 3
'hellohellohello'
>>> print("-" * 30)
------------------------------

1.3字符串格式化输出

老用法不提倡:

>>> "I like %s" % "python"
'I like python'
>>> "I like %s" % "Pascal"
'I like Pascal'

新用法提倡:

>>> "I like {0} and {1}".format("python","cangloshi")
'I like python and cangloshi' >>> "I like {0:10} and {1:>15}".format("python","canglaoshi")
'I like python and canglaoshi'
#{0:10} 为python预留10个字符,{1:>15}右对齐预留15字符 >>> "I like {0:^10} and {1:^15}".format("python","canglaoshi")
'I like python and canglaoshi '
#居中显示 >>> "I like {0:.2} and {1:^10.4}".format("python","canglaoshi")
'I like py and cang '
#显示第一个元素的前连个字符,第二个元素占10个字符 居中显示前4个元素 >>> "She is {0:d} years old and the breast is {1:f}cm".format(28,90.143598)
'She is 28 years old and the breast is 90.143598cm'
#数字操作 >>> "She is {0:4d} years old and the breast is {1:6.2f}cm".format(28,90.143598)
'She is 28 years old and the breast is 90.14cm'
#变量1占用4字节默认右对齐,变量2占用6字节右对齐,保留小数2位 >>> "She is {0:04d} years old and the breast is {1:06.2f}cm".format(28,90.143598)
'She is 0028 years old and the breast is 090.14cm'
#位数不足用0补 >>> "I like {lang} and {name}".format(lang="python",name='canglaoshi')
'I like python and canglaoshi' >>> data = {"name":"Canglaoshi","age":28}
>>> "{name} is {age}".format(**data)
'Canglaoshi is 28'
#字典用法

1.4常用字符串方法

dir(str)
#获取字符串所有方法
help(str.isalpha)
#多去方法帮助

1)判断是否全是字母

2)根据分隔符分割字符串

3)去掉字符串两头的空格

4)字符大小写转换

S.upper()     #S中的字母转换为大写
            S.lower()     #S中的字母转换为小写
            S.capitalize()   #将首字母转换为大写
            S.isupper()   #判断S中的字母是否全是大写
            S.islower()   #判断S中的字母是否全是小写
            S.istitle()   #判断S是否是标题模式,即字符串中所有的单词拼写首字母为大写,且其它字母为小写

5)用join拼接字符串

6)替换字符串

te = te.replace('test','OK')

>>> "python".isalpha() #判断是否全是字母
True
>>> "python2".isalpha()
False
>>> a = "I LOVE PYTHON"  #按照空格分割,生成列表
>>> a.split(" ")
['I', 'LOVE', 'PYTHON']
>>> b = "www.itdiffer.com"
>>> b.split(".")
['www', 'itdiffer', 'com']
>>> b = " hello "
>>> b.strip() #去掉两边的空格
'hello'
>>> b #未改变字符串本身
' hello '
>>> b.lstrip() #去掉左边空格
'hello '
>>> b.rstrip() #去掉右边空格
' hello
>>> a = "TAJZHANG"
>>> a.istitle() #判断大写,返回布尔值
False
>>> a = "tAJZHANG"
>>> a.istitle()
False
>>> a = "Taj,Zhang"
>>> a.istitle()
True
>>> a = "This is a Book"
>>> a.istitle()
False
>>> b = a.title()
>>> b
'This Is A Book'
>>> b.istitle()
True
>>> a = "Tajzhang"
>>> a.isupper()
False
>>> a.upper().isupper() #全大写判断
True
>>> a.islower()
False
>>> a.lower().islower() #全小写判断
True
>>> b = 'www.itdiffer.com'
>>> c = b.split(".")
>>> c
['www', 'itdiffer', 'com']
>>> ".".join(c)
'www.itdiffer.com'
>>> "*".join(c)
'www*itdiffer*com'

1.5字符编码

计算机中的编码:ASCLL、Unicode、UTF-8、gbk、gbk2312

python3中默认就是utf8不需要声明,python2中开头声明'# -*- coding: utf-8 -*-' 显示中文才不会报错

>>> import sys
>>> sys.getdefaultencoding() #查看目前的编码
'utf-8'
>>> ord("Q") #ASCLL码互转
81
>>> chr(81)
'Q'

pass 55页

老齐python-基础2(字符串)的更多相关文章

  1. Python基础数据类型-字符串(string)

    Python基础数据类型-字符串(string) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版 ...

  2. Python基础(二) —— 字符串、列表、字典等常用操作

    一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 二.三元运算 result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为 ...

  3. python基础、字符串和if条件语句,while循环,跳出循环、结束循环

    一:Python基础 1.文件后缀名: .py 2.Python2中读中文要在文件头写: -*-coding:utf8-*- 3.input用法      n为变量,代指某一变化的值 n = inpu ...

  4. Python基础__字符串拼接、格式化输出与复制

    上一节介绍了序列的一些基本操作类型,这一节针对字符串的拼接.格式化输出以及复制的等做做详细介绍.一. 字符串的拼接 a = 'I', b = 'love', c = 'Python'. 我们的目的是: ...

  5. python基础类型—字符串

    字符串str 用引号引起开的就是字符串(单引号,双引号,多引号) 1.字符串的索引与切片. 索引即下标,就是字符串组成的元素从第一个开始,初始索引为0以此类推. a = 'ABCDEFGHIJK' p ...

  6. Python基础二字符串和变量

    了解一下Python中的字符串和变量,和Java,c还是有点区别的,别的不多说,上今天学习的代码 Python中没有自增自减这一项,在转义字符那一块,\n,\r\n都是表示回车,但是对于不同的操作系统 ...

  7. Python基础之字符串和编码

    字符串和编码 字符串也是一种数据类型,但是字符串比较特殊的是还有个编码问题. 因为计算机自能处理数字,如果徐娅处理文本,就必须先把文本转换为数字才能处理,最早的计算机子设计时候采用8个比特(bit)作 ...

  8. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  9. Python开发【第一篇】Python基础之字符串格式化

    字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-310 ...

随机推荐

  1. ubuntu下自动备份mysql数据库

    转载自:Mayi mysql的安装目录为:/var/lib/mysql 下面咱们来一起完成自动备份mysql. 备份目录为:/home/mydb 并且在每天下午18:30分以mysqldata_201 ...

  2. ActiveMQ教程(消息发送和接受)

    一 环境的搭建 version为你的版本号 如果你是普通的项目的话,创建一个lib文件夹,导入相应的jar包到你的lib中,jar包为:activemq-all-{version}.jar.log4j ...

  3. C++复习15.指针知识

      C++复习15.指针知识 4.指针知识      在Tencent 笔试和面试中都考到了 C/C++中的指针知识,因为自己很不喜欢使用指针,所以才开始学习 Java的,但是现在看来还是躲不掉的,所 ...

  4. java中join用法

    今天又把join的用法大概看了一下,其实理解起来,还是比较简单.用个简单的例子说明一下吧. 1.通过下面的例子,可以看到说出结果中首先全部是是Thread-1,之后才会是Thread-2,这是因为在主 ...

  5. PentesterLab-From SQL Injection to Shell

    一.打开靶机ip出现如下界面: 随便点一下,点到test链接,访问了如下url:http://192.168.109.131/cat.php?id=1,这里出现了一个id,常规思路这个位置可能有sql ...

  6. WIFI 基础知识

    转载自:wifi基本知识 如侵犯您的版权,请联系:2378264731@qq.com 1. IE802.11简介 标准号 IEEE 802.11b IEEE 802.11a IEEE 802.11g ...

  7. L148

    You don't know about real loss, because that only occurs when you love something more than you love ...

  8. Linux之FTP服务

     一.ftp服务 ftp是一个文件传输协议(File Transfer Protocal).lftp相当于一个浏览器,用来向服务器发送请求的. 进行FTP服务的相关操作的时候,要先修改 vim /et ...

  9. openoffice在连接时有错误,无法连接上

    openoffice在连接时有错误,无法连接上 报如下错误: java.net.ConnectException: connection failed: socket,host=localhost,p ...

  10. Android程序员学WEB前端(1)-HTML(1)-标准结构常用标签-Sublime

    转载请注明出处:http://blog.csdn.net/iwanghang/article/details/76522043觉得博文有用,请点赞,请评论,请关注,谢谢!~ 8月份了,换工作有2个月了 ...