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页

转载于:https://www.cnblogs.com/Taj-Zhang/p/7406876.html

老齐python-基础2(字符串)相关推荐

  1. python基础实例-Python基础之字符串常见操作经典实例详解

    本文实例讲述了Python基础之字符串常见操作.分享给大家供大家参考,具体如下: 字符串基本操作 切片 # str[beg:end] # (下标从 0 开始)从下标为beg开始算起,切取到下标为 en ...

  2. 《每天五分钟冲击python基础之字符串练习题》(七)

    前言 相信通过上两节课的学习,同学们都已经能轻松掌握了,python字符串的入门和字符串的深入了,(ps:如果还没有学习的同学,请到这里先学习,再来看这节课喔!<每天五分钟冲击python基础之 ...

  3. 字符串从右截取_跟运维组学Python基础day04(字符串str的索引和切片)

    内容回顾 跟运维组学Python基础 day03 格式化输出 %s name = input('Pleases input your name: ') # Zanaoprint('My name is ...

  4. 带你学python基础:字符串

    还记得学习 C 语言的时候吗,是不是每天都在控制台程序上玩耍,那时发现编程太没意思了,就只能玩这些东西吗? 后来,发现其实,外面的世界还是非常的广阔的,但是,今天,既然是 python 基础,所以我们 ...

  5. Python 基础数据类型 -字符串(str)的详细用法

    字符串是编程中最重要的数据类型,也是最常见的 1.字符串的表示方式 -单引号' ' 双引号 " " 多引号 """ """ ...

  6. python将姓王的都改成老王_老王Python基础+进阶+项目篇(高清无密)

    老王Python教程 基础篇 基础篇1-福利课python先入为主上 基础篇2-福利课-python先入为主下篇 基础篇3-虚拟机安装xubuntu开发环境 基础篇4-linux基本命令以及开发环境 ...

  7. python基础之字符串(七)

    文章目录 1.python字符串 2.demo 3.字符串输入 4.切片 5.字符串常见操作 6.Python转义字符 7.Python 字符串格式化 8.Python三引号 9.Unicode 字符 ...

  8. python pymysql cursors_老雷python基础教程之pymysql学习及DB类的实现

    老雷python教程之pymysql学习及DB类的实现 CREATE TABLE `sky_guest` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` ...

  9. Python 基础系列--字符串与编码

    一旦走上编程这条路,如果不把编码这个问题搞清楚,那么它会像幽灵般纠缠你整个职业生涯. 字符串在编程中是使用频率最高的数据类型,像 web 网站中显示的中英文信息,使用记事本打开一个文本文件所看到的内容 ...

最新文章

  1. 【leetcode】力扣刷题(1):两数之和(Go、Python)
  2. GdiPlus[44]: IGPGraphics (三) 关于文本绘制续 - IGPStringFormat
  3. go语言的iota是什么意思_关于Golang中的iota
  4. public lt;Tgt; void method,此地泛型的意思
  5. TensorFlow高层次机器学习API (tf.contrib.learn)
  6. 基于vue-cli配置手淘的lib-flexible + rem,实现移动端自适应
  7. 神奇的幻方(NOIP2015)(真·纯模拟)
  8. 移动端网页特效:左右滑动开关
  9. [py2neo]Ubuntu14 安装py2neo失败问题解决
  10. 怎么调出matlab的函数,matlab定义函数【搞定方法】
  11. jQuery (二)
  12. uk码对照表_这份中外衣服鞋码尺寸对照表,请收好!
  13. 中文NER的正确打开方式: 词汇增强方法总结 (从Lattice LSTM到FLAT)
  14. Linux iptables防火墙详解(一)——iptables基础知识
  15. hadoop集群常见问题解决
  16. Google guava之BiMap简介说明
  17. C语言实现定积分的计算
  18. 英雄联盟(LOL)3d模型显示
  19. Deepin 微信版本太低无法登录
  20. 有一个程序员男友是一种怎么样的体验?

热门文章

  1. 碧生源2021上半年业绩:三茶、减肥药品收入下降,非主营业务增长
  2. Oracle 11.2.0.4.0 Oracle Database 11g Release 2 Installer(步骤一)
  3. 微信小程序-图片清晰度修复
  4. 非标机械设计具体是什么
  5. python接收邮件
  6. Ubuntu/Linux Mint用上仿Win7/Win8主题
  7. 平板电脑与计算机连接网络,平板电脑怎么上网 教你轻松连接网络【图文教程】...
  8. 从 Java 到 Scala,再到 Kotlin
  9. 腾讯云安装xampp搭建WordPress个人博客(步骤详细,小白向)
  10. 44Pin测试夹具封装