python中的字符串及常规操作

python中的字符串

目标
  • 认识字符串
  • 下标
  • 切片
  • 常用操作

一.认识字符串

字符串是python中的基本数据类型

字符串输出
name='sssss'
print('我的名字%s'% name)
print(f'我的名字{name}')
1.3字符出输入
password =input('请输入你的密码')
print(f'您输入的密码是{password}')
print(type(password))
下标

下标又叫索引

str1='asdadad'
print(str1)
print(str1[0])
print(str1[1])
print(str1[2])


asdadad
a
s
d

三、 切片

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

注意
1不包含结束位置下标对应的数据,正负数均可
2步长是选取间隔,正负数均可,默认步长为1.

str1='0123456789'
print(str1[0:5:1]) #01234
print(str1[2:5:2]) #24
print(str1[2:5])   #234
print(str1[:5])    #01234
print(str1[2:])    #23456789
print(str1[:])     #0123456789

#负数测试
print(str1[::-1])  #9876543210步长为负数倒叙
print(str1[-4:-1])  #678 下标-1表示最后一个数据
#终极测试
print(str1[-4:-1:1]) #678
print(str1[-4:-1:-1])  #没有输出

四、常用的操作方法

字符串常用的有 查找、修改和判断三大类

4.1查找

find();检测某个子串是否包含这个字符串中,如果在返回这个子串开始的位值下标,否则返回-1.

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

注意:开始和结束下标省略,表示整个字符串序列查找

2快速体验
str1='hello world and itcast and ithema and python'
print(str1.find('and')) # 12
print(str1.find('and',15,30)) # 23
print(str1.find('ands')) #-1

index()

方法:是在字符串里查找子串第一次出现的位置,类似字符串的find方法,不过比find方法更好的是,如果查找不到子串,会抛出异常,而不是返回-1。

语法

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

快速体验

#index()
print(str1.index('and',15,30)) #23
#print(str1.index('ands'))  #报错

count

count() 返回某个字串在字符串中出现的次数

语法

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

快速体验

print(str1.count('and',15,30))  #1
print(str1.count('and')) #3
print(str1.count('ands'))  #0

rfind

rfind() 返回字符串最后一次出现的位置(从右向左查询),如果没有匹配项则返回-1

体验

print(str1.rfind('and'))  #34 从右侧开始

4.2修改

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

replace()

替换

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

快速体验

split

按照指定字符分割字符串

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

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

快速体验
#splict  分割返还一个列表;丢失分割字符
list1=str1.split('and',2)
print(list1)

['hello world ', ' itcast ', ' ithema and python']

join

用一个字符或字串合并成字符串,即是将多个字符串合并成一个新的字符串

语法
字符或字串.join( 多字符串组成的序列 )
快速体验
#3join 合并列表里面的字符串数剧为一个大字符串
mylist=['aa','bb','cc']
#aa....bb.......cc
new_list='....'.join(mylist)
print(new_list)

aa....bb....cc

大小写转换

capitalize()将字符串的第一个字符转换成大写
注意:capitalize()函数转换之后,只字符串第一个字符大写,其他字符都小写

new_str=str1.capitalize()
print(new_str)
#Hello world and itcast and ithema and python

title():将字符串每个单词首字母转换成大写。

print(str1.title())
#Hello World And Itcast And Ithema And Python

lower() 将字符串中大写转小写

print(str1.lower())
#hello world and itcast and ithema and python

upper()将字符串中小写转大写

print(str1.upper())
#HELLO WORLD AND ITCAST AND ITHEMA AND PYTHON

删除空白字符

lstrip() 删除字符串左侧空白字符

str1='  hello world and itcast and ithema and python  '
new_str=str1.lstrip()
print(new_str)

rsplit() 删除字符串右侧空白字符

new_str1=str1.rstrip()
print(new_str1)

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

new_str2=str1.strip()
print(new_str2)


hello world and itcast and ithema and python  
  hello world and itcast and ithema and python
hello world and itcast and ithema and python

字符串对其

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

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

在这里插入图片描述
rjust()
在这里插入图片描述
center()
在这里插入图片描述

判断

startswith() 检查字符串是否是以指定字串开头,是则返回True,否则返回False.如果设置开始和结束位置下标。则在指定范围检查

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

快速体验

str1='hello world and itcast and ithema and python  '
print(str1.startswith('hello'))

endswith()

print(str1.endswith('python'))

isalpha()判断是否都是字母

print(str1.isalpha()) #false 有空格

isdigit() 判断是否都是数字
isalnum();如果字符串至少又一个字符并且所有字符都是字母或者数字则返回true,否则返回false
isspace()判断是不是空白

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拉格朗日(Lagrange)

手敲不易,谢谢各位老板,打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值