Python字符串操作

内容

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

一、认识字符串

字符串是Python中最常用的数据类型,我们一般使用引用号来创建字符串。创建字符串很简单,只要为变量分配一个值即可。

1.1 字符串特征

  • 单引号
  • 双引号
  • 三引号
a = 'hello world '
b = "hello world"
c = ''' hello world '''
d = """ hello 
world """
# 三引号支持回车换行 打印输出也是回车换行

#  输出I'm TOM
# e='I'm TOM' 出错
e= " I'm TOM " #或者
e= 'I\'m TOM' #转义字符'

1.2字符串输出

print('hello world')
name = 'hello world'
print('我的名字是%s' % name)
print(f'我的名字是{name}')

字符串输入

在Python 中,使用input()接受用户输入信息,类型为字符串类型

字符串下标

a = 'hello'
pirnt(a[0]) #结果为 h

切片

切片是只对操作的对象截取区中一部分的操作。字符串列表元组都支持切片操作。

3.1语法
序列[开始位置下标:结束位置下标:步长]
name = 'hello world'
print('我说的第一句话是%s' % name)
print(name[2:7]) # 左闭右开的取值
print(name[:7])
print(name[2:])
print(name[::-1])
print(name[-4:-1])
print(name[-4:-1:-1]) # 无法选取,选取方向和步方向不同
print(name[-1:-4:-1]) # 步长方向和选取方向相同,可以选取

常用操作方法

字符串常用操作方法有操作方法有查找、修改、判断三大类

查找

  • find():检测某个子串是否包含在这个字符串中,如果在返回这个子串开始的位置下
# 字符串序列.find(字串,开始位置下标,结束位置下标)
myStr = "hello world and itcast and Python"
print(myStr.find('and')) # 返回值 12
print(myStr.find('and', 15, 30)) # 返回值 23
print(myStr.find('ands')) # 返回值 -1 字串不存在
  • index()
print(myStr.index('and')) # 返回值和find相同
print(myStr.index('ands')) # 报错
  • count()
print(myStr.count('and', 12, 23)) # 返回值 1 左闭右开查找
print(myStr.count('and')) # 返回值 2
print(myStr.count('ands')) # 返回值 0
  • rfind()
  • rindex()
    从右往左查找

修改

  • replace():替换
    语法:
# 字符串.replace(新字串,旧字串,替换次数)
new_mystr = myStr.replace('and', 'or')
print(myStr)
print(new_mystr)

默认全部替换,原字符串是不可变数据类型,.repalce()返回一个新的字符串

  • split():字符串分割
# 字符串序列.split(分割字符,num)
listStr = myStr.split('and', 1) # 返回一个列表
print(listStr) # 丢失分隔符 即 'and'
  • join():用一个原有字符串和另一个字符串合并
# 字符串或者字串.join(多字符串组成的序列)
containerList = ['You', 'me', 'god']
temp = ' and '
tempStr = temp.join(containerList)
# tempStr2 = ''.join(containerList) 空连接
print(tempStr)
#输出结果 You and me and god
  • capitalize():将字符串第一个字符变成大写
  • title():将字符串每一个单词的第一个字符变成大写
  • lower:将字符串中大写转小写
  • upper():将字符串中小写转大写
tempStr1 = myStr.capitalize()
tempStr2 = myStr.title()
tempStr3 = myStr.lower()
tempStr4 = myStr.upper()
print(tempStr1)
print(tempStr2)
print(tempStr3)
''' 打印输出 
Hello world and itcast and python
Hello World And Itcast And Python
hello world and itcast and python
HELLO WORLD AND ITCAST AND PYTHON
'''
  • lstrip():删除字符串左侧空白字符
  • rstrip():删除字符串右侧的空白字符
  • strip():删除左右所有空白字符
myStr = "  hello world and itcast and Python  "
temptStr1 = myStr.lstrip()

temptStr2 = myStr.rstrip()

temptStr3 = myStr.strip()

print(temptStr1)
print(temptStr2)
print(temptStr3)
'''
输出结果
hello world and itcast and Python  
  hello world and itcast and Python
hello world and itcast and Python
'''
  • ljust():左对齐
  • rjust():右对齐
  • center():中心对齐
# 在Python console中进行测试
myStr = 'hello'
myStr
'hello'
myStr.ljust(10)
'hello     '
myStr.ljust(10,'.')
'hello.....'
myStr.rjust(10,',')
',,,,,hello'
myStr.center(11,'.')
'...hello...'

判断

判断真假,返回结果为布尔值

  • startswith():判断是否以某个子串开头
  • endswith():判断是否以某个子串结尾
# 字符串数列.startswith(字串,开始下标,结束下标)

  • isalpha():如果非空字符串都是字符
  • isdigit():如果字符串只包含数字则返回True
  • isalnum():如果字符串至少有一个字符,并且所有字符都是字母或者数字,则返回True
  • isspace():只包含空白
# 实例 判断一个字符串中除了空格外只有字母

# 去除字符串两端的空格字符
tempStr = myStr.strip()
# 以空格为分隔符进行单词提取
tempList = tempStr.split(' ')

print(tempList)
# 将所有单词进行连接
tempStrJoin = ''.join(tempList)

print(tempStrJoin)
# 判断是否全部是字母
print(tempStrJoin.isalpha())

'''
打印结果
['hello', 'world', 'and', 'itcast', 'and', 'Python']
helloworldanditcastandPython
True
'''
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值