python中的字符串及常规操作

简介: isalnum();如果字符串至少又一个字符并且所有字符都是字母或者数字则返回true,否则返回false

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(长度,填充字符)

20200129102554296.png


rjust()


20200129102617447.png


center()


20200129102627905.png


判断


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()判断是不是空白

相关文章
|
6月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
410 100
|
6月前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
575 99
|
6月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
6月前
|
开发者 Python
Python f-strings:更优雅的字符串格式化技巧
Python f-strings:更优雅的字符串格式化技巧
|
6月前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
6月前
|
Python
使用Python f-strings实现更优雅的字符串格式化
使用Python f-strings实现更优雅的字符串格式化
|
7月前
|
索引 Python
python 字符串的所有基础知识
python 字符串的所有基础知识
426 0
|
7月前
|
Python
Python字符串center()方法详解 - 实现字符串居中对齐的完整指南
Python的`center()`方法用于将字符串居中,并通过指定宽度和填充字符美化输出格式,常用于文本对齐、标题及表格设计。
|
7月前
|
Python
Python中的f-string:更简洁的字符串格式化
Python中的f-string:更简洁的字符串格式化
395 92

推荐镜像

更多