5.1、字符串:

1、n1 = "lc" n2 = 'root' n3 = """chang""" n4='''tom'''

2、井号(#)表示单行注释,""" """或''' '''表示多行注释也表示字符串

3、在paycharm中选中代码按"ctrl + ?"键表示多行单行注释

4、字符串和子字符串的区别:

"张"是字符

"张三丰"是字符串

"张三"是"张三丰"字符串中的子序列

5、字符串的特点:

(1)字符串是不可变数据类型(不可以增加,删除、修改元素)

(2)字符串元素是有序排列的

(3)字符串元素可以使用for循环进行遍历

5.2、字符串运算:

1、加法:

n1 = "a"

#赋值

n2 = "b"

n4 = "c"

n3 = n1 + n2 + n4

# "abc"

#字符串一旦创建,不可修改

# 一旦修改或者拼接,都会造成重新生成字符串

2、乘法:

n1 = "a"

n3 = n1 * 3

#aaa

5.3、字符串方法:

1、首字母大写,其余的都小写:

test = "lC"

v = test.capitalize()

print(v)

2、所有变小写:

test = "lc"

v1 = test.casefold()

print(v1)

#casefold更厉害,很多未知的对相应变小写

v2 = test.lower()

print(v2)

3、设置宽度,并将内容居中:

# 20 代指总长度

# * 空白未知填充,一个字符,可有可无

v = test.center(20,"中")

print(v)

test = "lc"

v = test.ljust(20,"*")

print(v)

test = "lc"

v = test.rjust(20,"*")

print(v)

4、去字符串中寻找,寻找子序列的出现次数:

test = "lclcliulc"

v = test.count('lc')

print(v)

test = "liuchangliuchang"

v = test.count('iu',5,6)

print(v)

#取m<=下标<M内的子序列出现的次数,下标从0开始;

5、以什么什么结尾,以什么什么开始:

test = "liuchag"

v = test.endswith('l')

v = test.startswith('g')

print(v)

6、expandtabs,从左往右说20个字符,字符不到20又遇到了"\t"用空格补齐到20:

test = "username\temail\tpassword\nlc1\tlc1@qq.com\t123"

v = test.expandtabs(20)

print(v)

7、从开始往后找,找到第一个子序列后,返回子序列第一个字符所在的下标:

test = "liuchag"

v = test.find('iu')

print(v)

#未找到 -1

8、格式化,将一个字符串中的占位符替换为指定的值:

test = 'i am {name}, age {a}'

print(test)

v = test.format(name='liuchang',a=23)

print(v)

9、字符串中是否只包含字母和数字:

test = "123"

v = test.isalnum()

print(v)

10、是否是字母,汉字:

test = "as2df"

v = test.isalpha()

print(v)

11、当前输入是否是数字:

test = "1"

v1 = test.isdecimal()

#最常用,判断1、2、3

v2 = test.isdigit()

#可以判断②

v3 = test.isnumeric()

#可以判断中文的“二”

print(v1,v2,v3)

12、是否存在不可显示的字符:

#\t 制表符

#\n 换行

test = "oiuas\tdfkj"

v = test.isprintable()

print(v)

13、判断是否全部是空格:

test = ""

v = test.isspace()

print(v)

14、判断是否是标题:

test = "Liu Chang"

v1 = test.istitle()

print(v1)

v2 = test.title()

print(v2)

#将字符转化为标题

v3 = v2.istitle()

print(v3)

15、将字符串中的每一个元素按照指定分隔符进行拼接:

test = "liuchang"

print(test)

#t = ' '

v = "_".join(test)

print(v)

16、判断是否全部是大小写 和 转换为大小写:

test = "Liuchag"

v1 = test.islower()

#判断字符串是否全为小写

v2 = test.lower()

#将字符串全部转为小写

#v1 = test.isupper()

#判断字符串是否全为大写

#v2 = test.upper()

#将字符串全部转为大写

print(v1, v2)

17、移除指定字符串:

test = " liu "

v=test.lstrip()

#移除字符串最左边的空格

#v=test.rstrip()

#移除字符串最右边的空格

#v=test.strip()

#移除字符串两边的空格,如果两边有\n\t参数也能去除

print(v)

test2 = "liu"

v1=test.rstrip("u")

#移除字符串最右边u

print(v1)

20、分割为三部分(包含指定的字符):

test = "12s12s12s12"

v = test.partition('s')

print(v)

#从最左边第一个指定字符分

v = test.rpartition('s')

print(v)

#从最右边第一个指定字符分

21、分割为指定个数(去除指定的字符):

test = "12s12s12s12"

v = test.split('s',2)

print(v)

#从字符串最左边分割;

v=test.rsplit('s',2)

print(v)

#从字符串最右边分割

v=test.rsplit('s')

print(v)

#将字符串中所有指定的字符串进行分割

22、分割,只能根据,true,false:是否保留换行

test = "asdfadfasdf\nasdfasdf\nadfasdf"

v = test.splitlines(False)

print(v)

23、以xxx开头,以xx结尾:

test = "backend1.1.1.1"

v = test.startswith('a')

print(v)

test.endswith('a)

24、大小写转换,大写转成小写,小写转成大写:

test = "Liu"

v = test.swapcase()

print(v)

25、字母,数字,下划线:标识符 def class:

a = "def"

v = a.isidentifier()

print(v)

#判断某个字符串是否是变量

26、将指定字符串替换为指定字符串:

test = "liuchang"

v = test.replace("iu",'ui')

print(v)

#将字符串中所有指定的字符替换成指定的字符

v = test.replace("iu",'ui',1)

print(v)

#将指定字符串替换为指定字符串,下标从第几位字符串开始

27、索引,下标,获取字符串中的某一个字符:

test = "妹子有种冲我来"

v = test[3]

print(v)

28、切片:

test = "妹子有种冲我来"

v = test[0:-1]

print(v)

#从字符串 0<=下标<下标结束,-1表示从右往左第1位数;

29、获取长度:

#Python3: len获取当前字符串中由几个字符组成

v = len(test)

print(v)

30、小结,7个基本常用字符串转换方法:

replace/find/join/strip/startswith/split/upper/lower/format

31、字符串格式化:

msg='i am %s my hobby is %d' % ("lc",18)

print(msg)

#%s表示格式化字符串,字符串可以接收一切数据类型

#%d表示格式化数字

#%+60s表示字符串之间的的间隔是多

tpl = "percent %.2f" % 99.97623

print(tpl)

#打印浮点数

tpl = 'percent %.2f %%' % 99.97623

print(tpl)

#打印百分比

tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}

print(tpl)

#传入字典

print('root','x','0','0',sep=':')

#将字符串以什么字符分离

tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)

#使用format格式

5、基本数据类型(str)的更多相关文章

  1. python基础之 基本数据类型,str方法和for循环

    1.概念 1.十进制转二进制,对2取余,余数倒序排列 2.字符串为空的时候,bool值为false,字符串非空就是True3.字符串转化成int时,必须是只包含数字才能转化.4.字符串转化成int时可 ...

  2. 编码,基本数据类型,str索引和切片,for循环

    1. 编码 1. 最早的计算机编码是ASCII. 美国人创建的. 包含了英文字母(大写字母, 小写字母). 数字, 标点等特殊字符!@#$% 128个码位 2**7 在此基础上加了一位 2**8 8位 ...

  3. python基础复习-1-2 数据类型-str、list、tuple、dict

    数据类型 数字 引号: 123 数值 '123' 字符串 整数:ini long 范围:(-2**31 - 2**31) num = 123 长整型 long (L) num = 123L 浮点型:f ...

  4. 记录我的 python 学习历程-Day03 数据类型 str切片 for循环

    一.啥是数据类型 ​ 我们人类可以很容易的分清数字与字符的区别,但是计算机并不能呀,计算机虽然很强大,但从某种角度上看又很傻,除非你明确的告诉它,1是数字,"汉"是文字,否则它是分 ...

  5. Python基础数据类型str字符串

    3.3字符串str ' ' 0 切片选取 [x:y] 左闭右开区间 [x:y:z] 选取x到y之间 每隔z选取一次(选取x,x+z,....) z为正 索引位置:x在y的左边 z为负 索引位置:x在y ...

  6. Python基本数据类型——str

    字符串常用操作 移除空白 分割 长度 索引 切片 class str(basestring): """ str(object='') -> string Retur ...

  7. Python学习day3 数据类型Ⅰ(str,int,bool)

    day3  数据类型 @上节内容补充 每周一个思维导图-xmind.exe in / not in #示例:(是否包含敏感字符)while True:    text = input('请输入你要说的 ...

  8. day3:数据类型 str

    1,int 一个数字占用的bit数目 i = 2 print(i.bit_length()) i = 3 print(i.bit_length()) i = 5 print(i.bit_length( ...

  9. Python数据类型-str,list常见操作

    一.字符串操作 语法:字符串名.startwith('字符串') 功能:判断字符串里是否以xxx开头 范例: 扩展:从控制台接收输入居住地址,如果地址以北京市开头,则输出北京人口,否则输入非北京人口. ...

随机推荐

  1. [bug] IDEA Maven 项目 Module 不加粗,无法编译

    参考 https://blog.csdn.net/qq_42479920/article/details/102859244

  2. 保存 yum 下载的软件包并制作成本地 yum 源

    保存 yum 下载的软件包并制作成本地 yum 源 实验对象 CentOS 7 yum 安装 nginx (nginx必须使用第三源才能安装:redhat8版本的则不需要,官网源自带nginx软件包) ...

  3. jolokia配置Java监控

    wget http://search.maven.org/remotecontent?filepath=org/jolokia/jolokia-jvm/1.3.6/jolokia-jvm-1.3.6- ...

  4. linux .tar.xz 文件解压和压缩

    场景:centos7.0下文件格式为xxx.tar.xz,解压和压缩命令: 压缩 tar -Jcf linux-3.10.0-123.13.1.el7.tar.xz(文件名) linux-3.10.0 ...

  5. python基础之字符串类型

    一.python字符串类型概述 定义:在单引号\双引号\三引号内,由一串字符组成 name='Test' name = 'test' print(type(name)) --------------- ...

  6. linux环境下时区无法设置(UTC无法更改为CST)的问题解决

    在进行linux下修改时区的时候 总是修改不了 修改成 Asia/Shanghai  但是 时区总是 +0000 却不是想要的+0800 按照网上的方法 A方法:tzselect:执行tzselect ...

  7. #undef 与 exit(0) 使用

    #undef  与 #defined 反,实际使用中较多的是当你需要使用自己定义的标准C里面已经的函数时可以这样操作: exit(0)和exit(1)是系统判断函数是否有正常的退出,一般0表示正常的退 ...

  8. 10.13 nc:多功能网络工具

    nc命令 是一个简单.可靠.强大的网络工具,它可以建立TCP连接,发送UDP数据包,监听任意的TCP和UDP端口,进行端口扫描,处理IPv4和IPv6数据包.     如果系统没有nc命令,那么可以手 ...

  9. urllib2连接超时设置

    #urllib2设置超时 #获取网页的源码 def getHtml(url,i): if i > 2: return try: req = urllib2.Request(url) time.s ...

  10. Lidar激光雷达市场

    Lidar激光雷达市场 近年来,激光雷达技术在飞速发展,从一开始的激光测距技术,逐步发展了激光测速.激光扫描成像.激光多普勒成像等技术,如今在无人驾驶.AGV.机器人等领域已相继出现激光雷达的身影. ...