Python:字符串简介和基本使用

本文介绍了Python中字符串的基本概念,如变量定义、不同类型字符串的区别,以及字符串的切片、下标、查找、修改等常用操作方法。强调了字符串是不可变数据类型的特点,并演示了如何使用split(),join()等函数处理字符串。
摘要由CSDN通过智能技术生成

认识字符串

a = 'hello ' \
    'world'
print(a)
print(type(a))

b = "TOM"
print(type(b))

# 三引号
e = '''i am TOM'''
print(type(e))

f = """I 
am TOM"""
print(type(f))
print(f)

# I'm TOM
c = "I'm TOM"
print(c)
print(type(c))

# d = 'I'm TOM'
d = 'I\'m TOM'
print(d)
print(type(d))

字符串的输出

print("hello world")

name = 'ROSE'
# 我的名字是TOM
print('我的名字是%s' % name)
print(f'我的名字是{name}')




字符串的输入

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




下标

str1 = 'abcdefg'
print(str1)

# 数据在程序运行过程中存储在内存
# ? 得到数据a字符, 得到数据b字符 --   使用字符串中某个特定的数据
# 这些字符数据从0开始顺序分配一个编号 -- 使用这个编号精确找到某个字符数据 -- 下标或索引或索引值
# str1[下标]
print(str1[0])
print(str1[1])


切片

str1 = 'abcdefg'
# 得到整个字符串数据
print(str1)

# 下标得到的是下标为某个数字的数据
print(str1[2])

# 得到abc 这3个数据该怎么办


体验切片

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

str1 = '012345678'
# print(str1[2:5:1])  # 234
# print(str1[2:5:2])  # 24
# print(str1[2:5])  # 234
# print(str1[:5])  # 01234 -- 如果不写开始,默认从0开始选取
# print(str1[2:])  # 2345678 -- 如果不写结束,表示选取到最后
# print(str1[:])  # 012345678 -- 如果不写开始和结束,表示选取所有

# 负数测试
# print(str1[::-1])  # 876543210 -- 如果步长为负数,表示倒叙选取
# print(str1[-4:-1])  # 567 -- 下标-1表示最后一个数据,依次向前类推

# 终极测试
# print(str1[-4:-1:1])  # 567
print(str1[-4:-1:-1])  # 不能选取出数据:从-4开始到-1结束,选取方向为从左到右,但是-1步长:从右向左选取
# **** 如果选取方向(下标开始到结束的方向) 和 步长的方向冲突,则无法选取数据


print(str1[-1:-4:-1])  # 876








字符串常用操作方法之查找

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

# 1. find()
# print(mystr.find('and'))  # 12
# print(mystr.find('and', 15, 30))  # 23
# print(mystr.find('ands'))  # -1 , ands子串不存在


# 2.index()
# print(mystr.index('and'))  # 12
# print(mystr.index('and', 15, 30))  # 23
# print(mystr.index('ands'))  # 如果index查找子串不存在,报错

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


# 4.rfind()
# print(mystr.rfind('and'))
# print(mystr.rfind('ands'))

# 5.rindex()
# print(mystr.rindex('and'))
# print(mystr.rindex('ands'))


字符串常用操作方法之修改

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

# 1. replace() 把and换成he  #** 说明replace函数有返回值,返回值是修改后的字符串
# new_str = mystr.replace('and', 'he')
# new_str = mystr.replace('and', 'he', 1)
# 替换次数如果超出子串出现的次数,表示替换所有这个子串
# new_str = mystr.replace('and', 'he', 10)
# # print(mystr)
# print(new_str)

# ***** 调用了replace函数后,发现原有字符串的数据并没有做到修改,修改后的数据是replace函数的返回值
# --- 说明 字符串是不可变数据类型
# 数据是否可以改变划分为 可变类型 和 不可变类型


# 2. split() -- 分割,返回一个列表, 丢失分割字符
# list1 = mystr.split('and')
# list1 = mystr.split('and', 2)
# print(list1)


# 3. join() -- 合并列表里面的字符串数据为一个大字符串
# mylist = ['aa', 'bb', 'cc']
#
# # aa...bb...cc
# new_str = '...'.join(mylist)
# print(new_str)

字符串常用操作方法之修改之非重点

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

# 1. capitalize() 字符串首字母大写
# new_str = mystr.capitalize()

# 2.title(): 字符串中每个单词首字母大写
# new_str = mystr.title()

# 3. upper():小写转大写
# new_str = mystr.upper()

# 4. lower(): 大写转小写
# new_str = mystr.lower()
# print(new_str)

mystr = "   hello world and itcast and itheima and Python   "
print(mystr)
# 1. lstrip(): 删除左侧空白字符
# new_str = mystr.lstrip()

# 2. rstrip(): 删除右侧空白字符
# new_str = mystr.rstrip()

# 3.strip():删除两侧空白字符
new_str = mystr.strip()
print(new_str)


字符串常用操作方法之判断

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

# 1. startswith(): 判断字符串是否以某个子串开头
# print(mystr.startswith('hello'))
# print(mystr.startswith('hel'))
# print(mystr.startswith('hels'))


# 2. endswith(): 判断字符串是否以某个子串结尾
# print(mystr.endswith('Python'))
# print(mystr.endswith('Pythons'))


# 3. isalpha(): 字母
# print(mystr.isalpha())

# 4. isdigit(): 数字
# print(mystr.isdigit())
mystr1 = '12345'
# print(mystr1.isdigit())

# 5. isalnum() : 数字或字母或组合
# print(mystr1.isalnum())
# print(mystr.isalnum())
mystr2 = 'abc123'
# print(mystr2.isalnum())


# 6.isspace(): 空白
# print(mystr.isspace())
mystr3 = '   '
# print(mystr3.isspace())


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员无羡

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值