字符串数据出现的意义

掌握字符串的定义和特性

能熟练掌握字符串常用操作,并了解其他工厂方法

字符串的定义和创建

  字符串是一个有序的字符集合,用于存储和表示基本的文本信息,  用引号“           ” 之间的内容就是字符串。

创建方式

  字符串 =  “    用引号引起来就行     ”

特性:

  按照从左到右的顺序定义字符集合,下表从0开始顺序访问,有序

  

提示:

  1.字符串单引号和双引号都无法取消特殊字符的含义,假如需要取消其特殊意义:

    则在其前面加上 r 如:name = r'I\thf'

  2. unicode字符串与 r 连用必须在 r 前面,如 name = ur'I\thf'

常用操作:

#索引

name = 'yan xia ting yu'

print(name[0])>>:yprint(name[-1])>>:uprint(name[-2])>>:y

str.index(element)#查找元素 假如存在 则返回索引 ,不存在报错。print(name.index('e'))    riase抛出异常:ValueError: substring not found

str.find(element)print(name.find('e'))#元素存在返回 索引,不存在 返回-1  不会抛出异常报错
print(name.find('y'))print(name.strip())#移除两边的空格print(name.lstrip())#移除左边的空格print(name.rstrip())#移除右边空格
name = '******   yan xia ting yu  *********'print(name.strip("*"))#移除两边的*字符
#字符串的长度print(name.__len__())#一般不用,是len()方法的实现方式>>:35print(len(name))>>:35
name = '******   yan xia ting yu  *********'
#字符串的替换print(name.replace('*','8'))#不指定则全部替换>>:888888   yan xia ting yu  888888888

print(name.replace('*','8',8))#指定替换个数>>:888888   yan xia ting yu  88*******

print(name.replace('*','8',-8))>>;888888   yan xia ting yu  888888888

print(name.replace('*','8',-1))>>:888888   yan xia ting yu  888888888

#切片
name = 'yanxiatingyu'print(name[0:])print(name[0::])print(name[0:7])#顾头不顾尾print(name[0:7:2])#步长为2print(name[4::2])#步长为2print(name[::-1])#反向步长  #取反print(name[:-7:-1])print(name[-2:-7:-1])print(name[-1:-7:-1])

>>:yanxiatingyu
>>:yanxiatingyu
>>:yanxiat
>>:ynit
>>:itny
>>:uygnitaixnay
>>:uygnit
>>:ygnit
>>:uygnit

#一些比较有用的函数

name = 'yanxiatingyuyanxiatingyu'upper_name='YANXIATINGYU'print(name.capitalize())print(upper_name.casefold())print(name.center(20,'8'))#第一个参数设置宽度,第二个:补全的字符:print(name.center(20))#不写默认为空格#原来的字符串居中,不够用字符补全print(name.count('y',0,7))#统计字符出现的次数,start_int_index,end_int_index设置查找范围print(name.count('y'))#不设定不限制:默认范围是全部

Yanxiatingyuyanxiatingyu
yanxiatingyu
yanxiatingyuyanxiatingyu
yanxiatingyuyanxiatingyu
1
4

import  hashlibhs=hashlib.md5()hs.update(name.encode('utf-8'))print(hs.hexdigest())>>:ecbf46328d68cca2a29e618186efd377

#字符串的格式输出

#format的三种玩法
res='{} {} {}'.format('egon',18,'male')
res='{1} {0} {1}'.format('egon',18,'male')
res='{name} {age} {sex}'.format(sex='male',name='egon',age=18)

#字符串的拼接

单独拿出来了 有兴趣可以去看看

# in 和 not in 成员运算

if 'y' in name:    print('存在')

if 'Y' not in name:    print('不存在')
#1、strip,lstrip,rstrip
#2、lower,upper
#3、startswith,endswith
#4、format的三种玩法
#5、split,rsplit
#6、join
#7、replace
#8、isdigit

#strip
name='*egon**'
print(name.strip('*'))
print(name.lstrip('*'))
print(name.rstrip('*'))

#lower,upper
name='egon'
print(name.lower())
print(name.upper())

#startswith,endswith
name='alex_SB'
print(name.endswith('SB'))
print(name.startswith('alex'))

#split
name='root:x:0:0::/root:/bin/bash'
print(name.split(':')) #默认分隔符为空格
name='C:/a/b/c/d.txt' #只想拿到顶级目录
print(name.split('/',1))

name='a|b|c'
print(name.rsplit('|',1)) #从右开始切分>>:['a|b', 'c']
#join tag=' ' print(tag.join(['egon','say','hello','world'])) 

#可迭代对象必须都是字符串 #replace name='yanxiatingyu say :i have ,my name is yanxiatingyu' 

print(name.replace('yanxiatingyu','88',1)) #isdigit:可以判断bytes和unicode类型,是最常用的用于于判断字符是否为"数字"的方法

age=input('>>: ') print(age.isdigit())

  

print(name.capitalize())#首字母大写print(name.swapcase())#全部大写print(name.istitle())#判断首字母是否大写
#find,rfind,index,rindex,count
name='egon say hello'
print(name.find('o',1,3)) #顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引
# print(name.index('e',2,4)) #同上,但是找不到会报错
print(name.count('e',1,3)) #顾头不顾尾,如果不指定范围则查找所有

#center,ljust,rjust,zfill
name='egon'
print(name.center(30,'-'))
print(name.ljust(30,'*'))
print(name.rjust(30,'*'))
print(name.zfill(50)) #用0填充

#expandtabs
name='egon\thello'
print(name)
print(name.expandtabs(1))

#captalize,swapcase,title
print(name.capitalize()) #首字母大写
print(name.swapcase()) #大小写翻转
msg='egon say hi'
print(msg.title()) #每个单词的首字母大写

#is数字系列
#在python3中
num1=b'4' #bytes
num2=u'4' #unicode,python3中无需加u就是unicode
num3='四' #中文数字
num4='Ⅳ' #罗马数字

#isdigt:bytes,unicode
print(num1.isdigit()) #True
print(num2.isdigit()) #True
print(num3.isdigit()) #False
print(num4.isdigit()) #False

#isdecimal:uncicode
#bytes类型无isdecimal方法
print(num2.isdecimal()) #True
print(num3.isdecimal()) #False
print(num4.isdecimal()) #False

#isnumberic:unicode,中文数字,罗马数字
#bytes类型无isnumberic方法
print(num2.isnumeric()) #True
print(num3.isnumeric()) #True
print(num4.isnumeric()) #True

#三者不能判断浮点数
num5='4.3'
print(num5.isdigit())
print(num5.isdecimal())
print(num5.isnumeric())
'''
总结:
    最常用的是isdigit,可以判断bytes和unicode类型,这也是最常见的数字应用场景
    如果要判断中文数字或罗马数字,则需要用到isnumeric
'''

#is其他
print('===>')
name='egon123'
print(name.isalnum()) #字符串由字母或数字组成
print(name.isalpha()) #字符串只由字母组成

print(name.isidentifier())
print(name.islower())
print(name.isupper())
print(name.isspace())
print(name.istitle())

python 基本数据类型 之 字符串的更多相关文章

  1. python基本数据类型之字符串(五)

    python基本数据类型之字符串(五) 遍历与查找 python中的字符串属于可迭代对象,通过一些方法可以遍历字符串中的每一个字符.而查找的方法主要有两个:find与index. 1.字符串的遍历 字 ...

  2. python基本数据类型之字符串(四)

    python基本数据类型之字符串(四) 判断方法 python中有一类用来判断字符串形式的方法,该类方法有两个特点:(1)方法名都是is开头(除了startswith和endswith):(2)返回值 ...

  3. python基本数据类型之字符串(三)

    python基本数据类型之字符串(三) 转换和判断方法 在python中,有一些内置方法可以将字符串转化特定形式,而与之对应的一些方法可以判断字符串是否符合某些形式.因此,在这篇文章中,笔者把转换方法 ...

  4. python基本数据类型之字符串(二)

    python基本数据类型之字符串(二) 替换方法 python中字符串的替换方法主要有:center.rjust\ljust.expandtabs.format\format_map(格式化).str ...

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

    Python基础数据类型之字符串 一.Python如何创建字符串 在python中用引号将一些文本包起来就构成了字符串(引号可以是单引号.双引号.单三引号,双三引号,它们是完全相同的) >> ...

  6. python自学笔记(二)python基本数据类型之字符串处理

    一.数据类型的组成分3部分:身份.类型.值 身份:id方法来看它的唯一标识符,内存地址靠这个查看 类型:type方法查看 值:数据项 二.常用基本数据类型 int 整型 boolean 布尔型 str ...

  7. Python基本数据类型之字符串、数字、布尔

     一.数据类型种类 Python中基本数据类型主要有以下几类: Number(数字) String(字符串) Bool (布尔) List(列表) Tuple(元组) Sets(集合) Diction ...

  8. Python基础 数据类型 (字符串、列表、字典、元组、集合、堆、栈、树)

    数据类型有整型.布尔.字符串.列表.字典.元组.集合.堆.栈和树. 1.整型: 整型就是数字 数字表示 python2 64位机器,范围-2^63~2^63-1 超出上述范围,python自动转化为l ...

  9. Python - 基本数据类型_str 字符串

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

  10. Python开发——数据类型【字符串】

    字符串定义 字符串是一个有序的字符的集合,用于存储和表示基本的文本信息 在Python中加了引号的字符,都被认为是字符串! 单引号.双引号.多引号之间的区别? 答案:单双引号没有区别 多引号的作用? ...

随机推荐

  1. http post 接口

    集团需求管理系统通过网状网与给各省公司需求管理系统进行交互.落地方为发起方提供访问的URL,发起方使用HTTP POST方法发送请求报文并得到应答报文,发起方作为落地方的HTTP客户端,落地方作为发起 ...

  2. 网络层——IP报文头介绍

    IP数据包也叫IP报文分组,传输在ISO网络7层结构中的网络层,它由IP报文头和IP报文用户数据组成,IP报文头的长度一般在20到60个字节之间,而一个IP分组的最大长度则不能超过65535个字节.  ...

  3. Java模板引擎之freemarker简介

  4. yii2.0 添加组件baidu ueditor

    下载uditor git clone https://github.com/BigKuCha/yii2-ueditor-widget.git 将下载的项目放到 common/wdigets目录上 修改 ...

  5. PHPActiveRecord validates

    validates_presence_of #检测是不是为空 为空的话可以抛出异常 *Model类: static $validates_presence_of = array( array('tit ...

  6. ss源码学习--工作流程

    ss的local端和server端的工作流程相似,因此复用了TCPRelay类和TCPRelayHandler类. 两端均是使用TCPRelay类监听连接,并使用TCPRelayHandler类处理请 ...

  7. js dongtai xianshi textarea zishu

    <form name="FORM" id="FORM" method="post" action="?action=$atc ...

  8. 每月IT摘录201810

    技术 1.Redis.对于单机实例,我们采用原生主从(Master-Slave)模式实现高可用,常规模式下对外仅暴露 Master 节点.由于使用原生 Redis,所以单机实例支持所有 Redis 指 ...

  9. pta l2-6(树的遍历)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 题意:给出一个二叉树的结点数目n, ...

  10. codeforces 722D Generating Sets 【优先队列】

    You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct positive ...