美文网首页
python04-字符串

python04-字符串

作者: 东邪_黄药师 | 来源:发表于2022-11-20 21:15 被阅读0次

字符串

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

a = 'hello world'
b = "abcdefg"
print(type(a))
print(type(b))

字符串特征

  • 1.一对引号字符串
name1 = 'Tom'
name2 = "Rose"
  • 2.三引号字符串
name3 = ''' Tom '''
name4 = """ Rose """
a = ''' i am Tom, 
        nice to meet you! '''

b = """ i am Rose, 
        nice to meet you! """

注意:三引号形式的字符串支持换行。
如果创建一个字符串I'm Tom?
使用转义符

c = "I'm Tom"
d = 'I\'m Tom'

字符串输出

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

字符串输入

name = input('请输入您的名字:')
print(f'您输入的名字是{name}')
print(type(name))

password = input('请输入您的密码:')
print(f'您输入的密码是{password}')
print(type(password))
image.png

下标

name = "abcdef"
print(name[1])
print(name[0])
print(name[2])
image.png

切片

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

语法

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

    1. 不包含结束位置下标对应的数据, 正负整数均可;
    1. 步长是选取间隔,正负整数均可,默认步长为1。
name = "0123456789"
print(name[2:5:1])  # 234
print(name[2:5])  # 234
print(name[:5])  # 01234
print(name[1:])  # 123456789
print(name[:])  # 0123456789
print(name[::2])  # 02468  这里的步长是2 开始和结束都没写
print(name[:-1])  # 012345678, 负1表示倒数第一个数据
print(name[-4:-1])  # 678
print(name[::-1])  # 9876543210
image.png

常用操作方法

1.查找

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

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

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

mystr = "hello world and itcast and itheima and Python"
print(mystr.find('and'))  # 12
print(mystr.find('and', 15, 30))  # 23
print(mystr.find('ands'))  # -1 , ands子串不存在

2.index()

  • 检测某个子串是否包含在这个字符串中,如果在返回这个子串开始的位置下标,否则则报异常。
mystr = "hello world and itcast and itheima and Python"
print(mystr.index('and'))  # 12
print(mystr.index('and', 15, 30))  # 23
print(mystr.index('ands'))  # 报错
image.png

3.count()

返回某个子串在字符串中出现的次数

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

mystr = "hello world and itcast and itheima and Python"

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

4.rfind()

  • rfind(): 和find()功能相同,但查找方向为==右侧==开始。

5.rindex()

  • rindex():和index()功能相同,但查找方向为==右侧==开始。

修改

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

1.replace():替换

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

mystr = "hello world and itcast and itheima and Python"
# 结果:hello world he itcast he itheima he Python
print(mystr.replace('and', 'he'))
# 结果:hello world he itcast he itheima he Python
print(mystr.replace('and', 'he', 10))
# 结果:hello world and itcast and itheima and Python
print(mystr)

注意:数据按照是否能直接修改分为==可变类型==和==不可变类型==两种。字符串类型的数据修改的时候不能改变原有字符串,属于不能直接修改数据的类型即是不可变类型。

2.split():按照指定字符分割字符串。

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

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

mystr = "hello world and itcast and itheima and Python"

# 结果:['hello world ', ' itcast ', ' itheima ', ' Python']
print(mystr.split('and'))
# 结果:['hello world ', ' itcast ', ' itheima and Python']
print(mystr.split('and', 2))
# 结果:['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
print(mystr.split(' '))
# 结果:['hello', 'world', 'and itcast and itheima and Python']
print(mystr.split(' ', 2))
image.png

注意:如果分割字符是原有字符串中的子串,分割后则丢失该子串。

3.join()

用一个字符或子串合并字符串,即是将多个字符串合并为一个新的字符串。
字符或子串.join(多字符串组成的序列)

list1 = ['chuan', 'zhi', 'bo', 'ke']
t1 = ('aa', 'b', 'cc', 'ddd')
# 结果:chuan_zhi_bo_ke
print('_'.join(list1))
# 结果:aa...b...cc...ddd
print('...'.join(t1))
image.png

4.capitalize()

将字符串第一个字符转换成大写。

mystr = "hello world and itcast and itheima and Python"

# 结果:Hello world and itcast and itheima and python
print(mystr.capitalize())

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

5.title()

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

mystr = "hello world and itcast and itheima and Python"
# 结果:Hello World And Itcast And Itheima And Python
print(mystr.title())

6.lower()

将字符串中大写转小写。

mystr = "hello world and itcast and itheima and Python"
# 结果:hello world and itcast and itheima and python
print(mystr.lower())

7.upper()

将字符串中小写转大写

mystr = "hello world and itcast and itheima and Python"

# 结果:HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON
print(mystr.upper())

8.lstrip()

删除字符串左侧空白字符。


image.png

9.strip()

删除字符串两侧空白字符。


image.png

10.ljust()

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

判断

所谓判断即是判断真假,返回的结果是布尔型数据类型:True 或 False。

1.startswith()

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

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

mystr = "hello world and itcast and itheima and Python   "
# 结果:True
print(mystr.startswith('hello'))
# 结果False
print(mystr.startswith('hello', 5, 20))

2.endswith()

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

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

mystr = "hello world and itcast and itheima and Python"

# 结果:True
print(mystr.endswith('Python'))

# 结果:False
print(mystr.endswith('python'))

# 结果:False
print(mystr.endswith('Python', 2, 20))
image.png

3.isalpha()

如果字符串至少有一个字符并且所有字符都是字母则返回 True, 否则返回 False。

mystr1 = 'hello'
mystr2 = 'hello12345'
# 结果:True
print(mystr1.isalpha())
# 结果:False
print(mystr2.isalpha())

isdigit():

如果字符串只包含数字则返回 True 否则返回 False。

mystr1 = 'aaa12345'
mystr2 = '12345'

# 结果: False
print(mystr1.isdigit())

# 结果:False
print(mystr2.isdigit())
image.png

isalnum()

如果字符串至少有一个字符并且所有字符都是字母或数字则返 回 True,否则返回 False。

mystr1 = 'aaa12345'
mystr2 = '12345-'

# 结果:True
print(mystr1.isalnum())

# 结果:False
print(mystr2.isalnum())

sspace()

如果字符串中只包含空白,则返回 True,否则返回 False。

mystr1 = '1 2 3 4 5'
mystr2 = '     '

# 结果:False
print(mystr1.isspace())

# 结果:True
print(mystr2.isspace())

相关文章

  • python04-字符串

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

  • python04-异常

    一. 了解异常 当检测到一个错误时,解释器就无法继续执行了,反而出现了一些错误的提示,这就是所谓的"异常"。 例如...

  • python04-装饰器

    装饰器 一个产品由多个部门协作开发,如分为底层开发和业务开发底层开发:主要负责系统底层的调用,方法大概100来个业...

  • Javascript知识点整合

    字符串 单行字符串: ‘字符串’或“字符串” 多行字符串: `多行字符串` 字符串操作: 字符串连接‘+’号 长度...

  • C++基础字符串

    字符串的构造 字符串特性描述 字符操作 字符串赋值 字符串连接 字符串比较 字符串查找 字符串替换 字符串删除 字...

  • iOS中的NSString与NSMutableString

    字符串的创建 字符串读写 字符串的比较 字符串的搜索 字符串截取 字符串替换 字符串与路径 字符串转换 NSMut...

  • iOS NSString用法总结

    字符串属性 字符串截取 字符串比较 字符串搜索 字符串拼接 字符串基本类型转换 字符串分行,分段 字符串列举(按条...

  • php 字符串常见方法汇总

    字符串拼接 字符串检索 字符串截取 字符串替换 字符串大小写转化 字符串转数组 字符串格式化

  • iOS 字符串截取、iOS 字符串替换、iOS 字符串分隔、iO

    iOS之字符串截取、iOS 字符串替换、iOS字符串分隔、iOS之字符串匹配、截取字符串、匹配字符串、分隔字符串 ...

  • PHP中字符串函数库常用函数解析 -- PHP 学习 (十一)

    常用字符串函数分类: 字符串长度, 字符串查找, 字符串大小写转换, 字符串截取, 字符串 ASCII, 字符串加...

网友评论

      本文标题:python04-字符串

      本文链接:https://www.haomeiwen.com/subject/ohbbxdtx.html