python字符串函数运算_Python字符串合计与运算函数,python,全及,操作

python字符串全及操作函数

1、字符串

一对引号字符串

name='Tom'

name="Rose"

三引号字符串

name='''Tom'''

name="""Rose"""

a='''i am Tom,

nice to meet you!'''

注意:三引号字符串支持换行

#如果创建一个字符串 I'm Tom

a="I'm Tom"

b='I\'m Tom'

1.1、字符串输出

直接输出

%s输出

f格式化输出

print('hello')

name='hello'

print('我的名字是%s'%name)

print(f'我的名字是{name}')

1.2、字符串输入

input()

#中文字符

name=input('请输入你的名字')

print(f'我的名字是{name}')

print(type(name))

#数字或符号或字母等

password=input('请输入你的密码')

print(f'我的密码是{password}')

print(type(password))

#type都是‘str’

1.3、下标(索引)

name='abcd'

print(name[1])#b

print(name[0])#a

下标从0开始

1.4、切片

切片:指对操作的对象截取其中一部分的操作。可用于

字符串

列表

元组

默认步长为1

不包含结束位置下标对应的数据

正负数均可

#语法:

序列[开始位置下标:结束位置下标:步长]

name='abcdefg'

print(name[2:5:1]) #cde

print(name[2:5]) #cde

print(name[:5]) #abcde

print(name[3:])#defg

print(name[-4:-1])#def

print(name[-4:])#defg

print(name[-4::-1])#dcba

1.5、字符串常用操作方法(函数)

字符串的常用操作方法:

查找、修改、判断。

1.5.1、查找

字符串的查找方法即查找子串在字符串中的位置或出现的次数

find():检查某个子串

是否包含

在这个字符串中,如果在,返回这个子串

开始的位置的下标

,否则返回-1

rfind():和find()功能相同,但查找方向从右开始

index():检查某个子串

是否包含

在这个字符串中,如果在,返回这个子串

开始的位置的下标

,否则

报异常

rindex():和index()功能相同,但查找方向从右开始

count():检查某个子串在这个字符串

出现的次数

,如果有,返回次数,如果没有,返回0

#语法:--开始和结束下标可以为空,即默认在整个字符串中查找

字符串序列.find(字串,开始位置下标,结束位置下标)

字符串序列.index(字串,开始位置下标,结束位置下标)

字符串序列.count(字串,开始位置下标,结束位置下标)

str="hello world and i love python and test"

print(str.find('and')) #12

print(str.find('and',13,35))#30

print(str.find('ands'))#-1

print(str.index('ands'))# 报异常

print(str.count('and'))#2

print(str.count('ands'))#0

1.5.2、修改

字符串的修改指的是 通过函数的形式修改字符串中的数据

(一).常见操作函数

replace():替换字符串,会返回一个

新字符串

,不会修改原字符串

split():按照指定字符分割字符串,会返回一个

列表

join():用一个字符或子串合并字符串,即是将多个字符串合并为一个新的字符串,返回一个

字符串

字符串是不可变类型!

#语法:

字符串序列.replace(旧子串,新子串,替换次数)

字符串序列.split(分割字符,num)

#num 表示分割字符出现的次数,即将来返回数据个数为num+1个

字符或子串.join(多字符或字符串组成的序列)

str = "hello world and i love python and test and java"

list=["aa","bb","cc"]

#replace-替换

str.replace("and","or")

print(str)#原字符串不变

new_str=str.replace("and","or")

print(new_str)#生成新字符串--替换成功

#split-切割

list_1=str.split("and")

list_2=str.split("and",2)

print(list_1)#[hello word,i love python,test,java]

print(list_2)#[hello word,i love python,test and java]

#join-合并

print(".".join(str))#h..e..l..l..o等等按照字符串中单个字符合并

print(".".join(list))#aa.bb.cc 按照列表中单个列表项合并

(二).大小写相关操作函数

capitalize():将字符串第一个字符转换成大写,其他字符变为小写

title():将字符串的每个单词首字母都变成大写,其他字符变为小写

lower():将字符串中所有大写转为小写

upper():将字符串中所有小写转为大写

swapcase():将字符串大小写互换

str = "hello world and i love PYTHON and TEST and JAVA"

print(str.capitalize())

#Hello world and i love python and test and java

print(str.title())

#Hello World And I Love Python And Test And Java

print(str.lower())

#hello world and i love python and test and java

print(str.upper())

#HELLO WORLD AND I LOVE PYTHON AND TEST AND JAVA

print(str.swapcase())

#HELLO WORLD AND I LOVE python AND test AND java

(三).删除空白字符操作函数

Istrip():删除字符串左侧空白字符

rstrip():删除字符串右侧空白字符

strip():删除字符串两侧空白字符

replace():可以用替换的思想去除所有空格

split()+join():可以分割后组合思想去除所有空格

str=" hello world "

print(str.lstrip())

#hello world

print(str.rstrip())

# hello world

print(str.strip())

#hello world

print(str.replace(" ",""))

#helloworld

print("".join(str.split(" ")))

#helloworld

(四).对齐函数

ljust():返回一个原子符串左对齐,并使用指定字符(默认空格)填充至对应长度的

新字符串

rjust():返回一个原子符串右对齐,并使用指定字符(默认空格)填充至对应长度的

新字符串

center():返回一个原子符串居中对齐,并使用指定字符(默认空格)填充至对应长度的

新字符串

#语法:

字符串序列.ljust(长度,填充字符)

字符串序列.rjust(长度,填充字符)

字符串序列.center(长度,填充字符)

str="hello"

print(str.ljust(10,"$"))

#hello$$$$$

print(str.rjust(10,"*"))

#*****hello

print(str.center(10,"%"))

#%%hello%%%

1.5.3、判断

字符串的判断即对字符串满足条件判断,返回结果是布尔值

(一).判断指定开头结尾

startswith():检查字符串是否以指定的字串开头,可设定开始位置和结束位置下标检查指定范围,可省略位置参数

endswith():检查字符串是否以指定的字串结尾,可设定开始位置和结束位置下标检查指定范围,可省略位置参数

字串可以以元组类型——满足其中之一即可

#语法:

字符串序列.startswith(字串,开始位置下标,结束位置下标)

字符串序列.endswith(字串,开始位置下标,结束位置下标)

str="hello world and i love python and test"

print(str.startswith('he')) #True

print(str.startswith('world',6)) #True

print(str.startswith(("hello","HELLO","Hello"))) #True

print(str.endswith("est")) #True

print(str.endswith("Test")) #False

(二).判断字符串内指定数据条件

isalpha():所有字符都是字母

isdigit():所有字符都是数字

isalnum():所有字符都是字母或者数字

islower():所有字符都是小写

isupper():所有字符都是大写

istitle():所有单词都是首字母大写

isspace():所有字符都是空白字符

s1="hello world"

s2="helloworld"

s3="123"

s4="hello123"

print(s1.isalpha()) #False -有空格

print(s2.isalpha()) #True

print(s3.isdigit()) #True

print(s4.isalnum()) #True

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值