1、字符串定义

就是引号里的内容   单引号、双引号、三引号

2、原始字符串
(1)‘\’转义
>>> print ("c:\now")
c:
ow
>>> print ("c:\\now")
c:\now
(2)字符串有很多反斜杠时需使用原始字符串,只需在字符串前面加r
>>> print (r'c:\now')
c:\now
3、字符串运算符

(1)+ 字符串拼接

>>> str1='i am'
>>> str2='a boy'
>>> str3=str1+str2
>>> str3
'i ama boy'

(2)* 字符串重复

>>> a="hello"
>>> a*2
'hellohello'

(3)[] 字符串截取

>>> a='hello world'
>>> a[1:-1:2]
'el ol'

(4)in/not in 判断字符串是否包含给定的字符串

>>> a='hello world'
>>> 'id' in a
False

4、字符串内建函数

split()分割字符串
casefold()将整个字符串的所有字符改为小写
center(width)将字符串居中。并使用空格填充至width的新字符串
ljust(width)返回一个左对齐的字符串,并使用空格填充至长度为width的新字符串
rjust(width)返回一个右对齐的字符串,并使用空格填充至长度为width的新字符串
count(sub,start,end)返回sub在字符串中出现的次数,start和end表示参数范围
encode()以encode指定的编码格式对字符串进行编码
endwith(sub,start,end)检查字符串是否以sub子字符串结束,返回true,否则返回false
expandtabs()把字符串的tab符号(\t)转换为空格,如不指定参数默认空格数是tabsize=8
find(sub,start,end)检查sub是否包含在字符串中,如果有则换回索引值,否则返回-1,start,end参数可选
isalnum()字符串至少有一个字符并且所有字符都是字母或者数字返回T入耳,否则返回fals
isalpha()都是字母返回True
islower()所有字符都是小写返回True
isupper()所有字母
istitle()所有字母只有单词首字母大写其他的都是小写
join(sub)以字符串作为分隔符,插入sub中
lower()转换所有的大写为小写
upper()转换所有的小写为大写
lstrip()去掉字符串中左边的空格
partition(sub)找到子字符串sub把这个字符串隔成3个,组成一个元祖
replace(old,new,count)将字符串中old子字符串换成new的子字符串,如果count指定则替换,不超过count次数
zfill(width)返回长度为width的字符串,原字符串右对齐,前边用0填充

capitalize()字符串首字母大写

# 1 首字母大写
                        # test = "aLex"
                        # v = test.capitalize()
                        # print(v)

# 2 所有变小写,casefold更牛逼,很多未知的对相应变小写
                        # v1 = test.casefold()
                        # print(v1)
                        # v2 = test.lower()
                        # print(v2)

# 3 设置宽度,并将内容居中
                        # 20 代指总长度
                        # *  空白未知填充,一个字符,可有可无
                        # v = test.center(20,"中")
                        # print(v)

# test = "alex"
                        # v = test.ljust(20,"*")
                        # print(v)

# test = "alex"
                        # v = test.rjust(20,"*")
                        # print(v)

# test = "alex"
                        # v = test.zfill(20)
                        # print(v)

# 4 去字符串中寻找,寻找子序列的出现次数
                        # test = "aLexalexr"
                        # v = test.count('ex')
                        # print(v)

# test = "aLexalexr"
                        # v = test.count('ex',5,6)
                        # print(v)

# 欠
                    # encode
                    # decode

# 5
                        # 以什么什么结尾
                        # 以什么什么开始
                        # test = "alex"
                        # v = test.endswith('ex')
                        # v = test.startswith('ex')
                        # print(v)

# 6 expandtabs,断句20,
                        # test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"
                        # v = test.expandtabs(20)
                        # print(v)

# 7 从开始往后找,找到第一个之后,获取其未知
                        # > 或 >=
                        # test = "alexalex"
                        # 未找到 -1
                        # v = test.find('ex')
                        # print(v)

# 8 index找不到,报错   忽略
                        # test = "alexalex"
                        # v = test.index('8')
                        # print(v)

# 9 格式化,将一个字符串中的占位符替换为指定的值
                        # test = 'i am {name}, age {a}'
                        # print(test)
                        # v = test.format(name='alex',a=19)
                        # print(v)

# test = 'i am {0}, age {1}'
                        # print(test)
                        # v = test.format('alex',19)
                        # print(v)

# 10 格式化,传入的值 {"name": 'alex', "a": 19}
                        # test = 'i am {name}, age {a}'
                        # v1 = test.format(name='df',a=10)
                        # v2 = test.format_map({"name": 'alex', "a": 19})

# 11 字符串中是否只包含 字母和数字
                        # test = "123"
                        # v = test.isalnum()
                        # print(v)
                        # str
                        
                    # 12 是否是字母,汉子
                        # test = "as2df"
                        # v = test.isalpha()
                        # print(v)

# 13 当前输入是否是数字
                        # test = "二" # 1,②
                        # v1 = test.isdecimal()
                        # v2 = test.isdigit()
                        # v3 = test.isnumeric()
                        # print(v1,v2,v3)

# 14 是否存在不可显示的字符
                        # \t   制表符
                        # \n   换行
                        # test = "oiuas\tdfkj"
                        # v = test.isprintable()
                        # print(v)

# 15 判断是否全部是空格
                        # test = ""
                        # v = test.isspace()
                        # print(v)

# 16 判断是否是标题
                        # test = "Return True if all cased characters in S are uppercase and there is"
                        # v1 = test.istitle()
                        # print(v1)
                        # v2 = test.title()
                        # print(v2)
                        # v3 = v2.istitle()
                        # print(v3)

# 17 ***** 将字符串中的每一个元素按照指定分隔符进行拼接
                        # test = "你是风儿我是沙"
                        # print(test)
                        # # t = ' '
                        # v = "_".join(test)
                        # print(v)

# 18 判断是否全部是大小写 和 转换为大小写
                        # test = "Alex"
                        # v1 = test.islower()
                        # v2 = test.lower()
                        # print(v1, v2)

# v1 = test.isupper()
                        # v2 = test.upper()
                        # print(v1,v2)
                    # 19
                        # 移除指定字符串
                        # 有限最多匹配
                        # test = "xa"
                        # # v = test.lstrip('xa')
                        # v = test.rstrip('9lexxexa')
                        # # v = test.strip('xa')
                        # print(v)

# test.lstrip()
                        # test.rstrip()
                        # test.strip()
                        # 去除左右空白
                        # v = test.lstrip()
                        # v = test.rstrip()
                        # v = test.strip()
                        # print(v)
                        # print(test)
                        # 去除\t \n
                        # v = test.lstrip()
                        # v = test.rstrip()
                        # v = test.strip()
                        # print(v)

# 20 对应关系替换
                        # test =  "aeiou"
                        # test1 = "12345"

# v = "asidufkasd;fiuadkf;adfkjalsdjf"
                        # m = str.maketrans("aeiou", "12345")
                        # new_v = v.translate(m)
                        # print(new_v)

# 21 分割为三部分
                        # test = "testasdsddfg"
                        # v = test.partition('s')
                        # print(v)
                        # v = test.rpartition('s')
                        # print(v)

# 22 分割为指定个数
                        # v = test.split('s',2)
                        # print(v)
                        # test.rsplit()

# 23 分割,只能根据,true,false:是否保留换行
                        # test = "asdfadfasdf\nasdfasdf\nadfasdf"
                        # v = test.splitlines(False)
                        # print(v)

#  24 以xxx开头,以xx结尾
                        # test = "backend 1.1.1.1"
                        # v = test.startswith('a')
                        # print(v)
                        # test.endswith('a)

# 25 大小写转换
                        # test = "aLex"
                        # v = test.swapcase()
                        # print(v)

# 26 字母,数字,下划线 : 标识符 def  class
                        # a = "def"
                        # v = a.isidentifier()
                        # print(v)

# 27 将指定字符串替换为指定字符串
                        # test = "alexalexalex"
                        # v = test.replace("ex",'bbb')
                        # print(v)
                        # v = test.replace("ex",'bbb',2)
                        # print(v)
                    ###################### 7个基本魔法 ######################
                    # join       # '_'.join("asdfasdf")
                    # split
                    # find
                    # strip
                    # upper
                    # lower
                    # replace
                    ###################### 4个灰魔法 ######################
                    # test = "郑建文妹子有种冲我来"

# 一、for循环
                        # for 变量名 in 字符串:
                        #     变量名
                        # break
                        # continue
                        
                        
                        # index = 0
                        # while index < len(test):
                        #     v = test[index]
                        #     print(v)
                        #
                        #     index += 1
                        # print('=======')

# for zjw in test:
                        #     print(zjw)

# test = "郑建文妹子有种冲我来"
                        # for item in test:
                        #     print(item)
                        #     break

# for item in test:
                        #     continue
                        #     print(item)

# 二、索引,下标,获取字符串中的某一个字符
                        # v = test[3]
                        # print(v)

# 三、切片
                        # v = test[0:-1] # 0=<  <1
                        # print(v)

# 四、获取长度
                        # Python3: len获取当前字符串中由几个字符组成
                        # v = len(test)
                        # print(v)

# 注意:
                    # len("asdf")
                    # for循环
                    # 索引
                    # 切片

# 五、获取连续或不连续的数字,
                        # Python2中直接创建在内容中
                        # python3中只有for循环时,才一个一个创建
                        # r1 = range(10)
                        # r2 = range(1,10)
                        # r3 = range(1,10,2)
                        # 帮助创建连续的数字,通过设置步长来指定不连续
                        # v = range(0, 100, 5)
                        #
                        # for item in v:
                        #     print(item)

##### 练习题:根据用户输入的值,输出每一个字符以及当前字符所在的索引位置 #####
                        # test = input(">>>")
                        # for item in test:
                        #     print(item)

# 将文字 对应的索引打印出来:
                        # test = input(">>>")
                        # print(test)   # test = qwe   test[0]   test[1]
                        # l = len(test) # l = 3
                        # print(l)
                        #
                        # r = range(0,l) # 0,3
                        # for item in r:
                        #     print(item, test[item]) # 0 q,1 w,2 e

# test = input(">>>")
                        # for item in range(0, len(test)):
                        #     print(item, test[item])

###################### 1个深灰魔法 ######################
                    # 字符串一旦创建,不可修改
                    # 一旦修改或者拼接,都会造成重新生成字符串

# name = "zhengjianwen"
                    # age = "18"
                    #
                    # info = name + age
                    # print(info)

转载于:https://www.cnblogs.com/fat-girl-spring/p/9121777.html

python字符串应用相关推荐

  1. python 字符串格式化是打印不同类型更简单一些

    Python 支持格式化字符串的输出 与 C 中 sprintf 函数一样的语法 下面写3中不同类型的数据合在一起打印 name = "张三丰" height = 1.88 wei ...

  2. Python——字符串大小写转化

    python字符串得一些用法: 1.输入一个字符串,将其单词首字母由小写变成大写 当所有字母都为大写时,结果也是将首字母变成大写,其余仍然是小写 2.将小写字母转化为大写字母,大写字母仍然转化为大写字 ...

  3. 真香!精心整理了 100+Python 字符串常用操作

    来源丨萝卜大杂烩 作者丨周萝卜 字符串作为平时使用最多的数据类型,其常用的操作我们还是很有必要熟记于心的,本文整理了多种字符串的操作的案例,还是非常用心,记得点赞收藏~ 字符串切片操作 test = ...

  4. Python字符串方法:字符串拼接、截取、转数字

    这节课程我们主要讲有关Python字符串的用法,包括字符串的拼接.字符串怎么转数字.字符串的格式化.字符串函数等内容. 1.Python字符串的拼接 拼接方式很简单-我们可以用"+" ...

  5. Python字符串居然可以这样玩 到底怎么做到的 年薪50w程序员揭晓

    Python如何比较字符串?由于字符串是Python中最常用的数据类型,所以我们考虑简化字符串比较操作.在本教程中,我们将介绍如何创建字符串对象,如何使用引号,最重要的是在Python中比较字符串的七 ...

  6. c int转字符串_【C++实现python字符串函数库】字符串匹配函数startswith与endswith

    [C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

  7. python输入字符串转换为公式_将python字符串转化成长表达式的函数eval实例

    爬一个网页时,要保存的数据都没有encode,就导致保存下来的中文都变成unicode了... 那么,怎么把一个表示字符串的unicode还原成unicode呢? 函数eval(expression) ...

  8. 如何检查一个Python字符串是否只包含数字?

    如何检查一个Python字符串是否只包含数字? python String类中有一个名为isdigit()的方法,如果字符串中所有字符都是数字且至少有一个字符,则返回true,否则返回false. p ...

  9. python 字符串按指定分隔符分割

    python 字符串按指定分隔符分割 def str_split(str1,str2):"""# 字符串分割:param str1:字符串:param str2:分隔符: ...

  10. python 案例串接_来撸串,一个案例轻松认识Python 字符串——翻转拼接游戏

    键盘输入一个字符串和数字,从头部或尾部截取指定数量的字符串,然后将其翻转拼接.将字符串在第n个字符处翻转拼接,例如输入,python,2;输出头部翻转:thonpy;尾部翻转:onpyth. 字符串是 ...

最新文章

  1. 个人作业-Alpha项目测试
  2. Windows Forms高级界面组件-快捷菜单
  3. 一切技术创新史都是数据史
  4. 如何正确使用SqlConnection
  5. VTK:超树网格源用法实战
  6. 模拟inode号耗尽、EXT和XFS类型文件恢复(详细图解)
  7. pandaboard ES学习之旅——2 ES环境搭建
  8. C语言用字符串sex储存,2005年计算机等级考试二级C语言全真标准预测试卷(2)
  9. Hadoop背景、模块介绍、架构
  10. 开学季,复旦老师教你玩转“0”“1”浪漫!| 人物志
  11. java hashmap 去重_java数组去重的两种方法
  12. Openlayer:学习笔记之交互
  13. Mybatis原生dao开发方法实现增删改查
  14. GCC编译器的使用方法
  15. 荔枝直播助手无法连接到服务器,荔枝FM直播助手pc客户端
  16. 莫纳什计算机专业优势,2020年去澳洲留学就读莫纳什大学计算机学院有哪些优势?...
  17. 软件分享系列之【AE 下载安装】并持续分享中...
  18. 信用卡 3D 认证是怎么回事
  19. linux系统怎么下载
  20. java jar 指定路径_java – 指定jar的类路径

热门文章

  1. Ext JS高级程序设计
  2. python print()内置函数
  3. 循环——批量处理数据
  4. J2EE后台UI系统框架搭建-EXTJs使用(4.1 GPL版本)
  5. java web服务器tomcat介绍【转载】
  6. 授权后接口调用(UnionID)
  7. Uploadify——学习(1):在Struts2的使用
  8. 在工作迷惘的寒冬感受亲情的温暖。
  9. RMAN 备份SHELL
  10. 解决SurfaceView渲染的各种疑难杂症