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] Job for network.service failed because the control process exited with error code

    原因 复制虚拟机,没有改网卡配置文件 参考 https://blog.csdn.net/dongfei2033/article/details/81124465

  2. /var/log/syslog日志usb接口

    p:~# cat /proc/bus/input/devices |grep usbP: Phys=usb-0000:00:05.1-1/buttonS: Sysfs=/devices/pci0000 ...

  3. 045.Python线程队列

    线程队列 1 基本语法和用法 put 往线程队列里防止,超过队列长度,直接阻塞 get 从队列中取值,如果获取不到,直接阻塞 put_nowait: 如果放入的值超过队列长度,直接报错(linux) ...

  4. python3 列表转换为字符串

    join将列表转换为字符串 list1 = ["张三","李四","王五"] a1 = ','.join(list1) print(a1) ...

  5. cgic: CGI的C函数库-(转自COS)

    下载回源码包以后,就3个文件:cgic.c      函数库capture.c   一个很简单的CGI例子,仅仅输出两行提示文字cgictest.c  一个演示读取form表单数据的CGI例子 首先在 ...

  6. Git 上传基本命令

    注意:操作要保证在对应文件夹中打开Git bash here (例如:clone项目后要cd到文件中,否则报"git提示没有git存储库") 1.创建一个git裸服务器 (git ...

  7. MySQL中InnoDB存储引擎的实现和运行原理

    InnoDB 存储引擎作为我们最常用到的存储引擎之一,充分熟悉它的的实现和运行原理,有助于我们更好地创建和维护数据库表. InnoDB 体系架构 InnoDB 主要包括了: 内存池.后台线程以及存储文 ...

  8. Java读取SQL server数据库

    要打开SQL server 的三个服务,然后再执行代码. package com.sql; import java.sql.SQLException; import java.sql.Statemen ...

  9. 201871030132-熊文婷 实验二 个人项目―《D{0-1}KP问题》项目报告

    项目 内容 课程班级博客链接 https://edu.cnblogs.com/campus/xbsf/2018CST 这个作业要求链接 https://www.cnblogs.com/nwnu-dai ...

  10. Linux基础服务——Bind DNS服务 Part1

    Linux基础服务--Bind DNS服务 Part1 DNS正向解析 实验环境: CentOS8.3.2011 IP地址:192.168.100.50 VMware虚拟环境 NAT网段 需要解析的区 ...