Python基本相关知识-09-字符串及其常用操作

本文介绍了Python中的字符串基本特征,包括不同类型的字符串及其类型检查。详细讲解了字符串的下标访问、切片操作以及一系列常用方法,如find、index、replace、split、join等,涵盖了字符串查找、替换、分割和格式化等方面的功能。

1.字符串

# 1.1字符串特征
 

name1 = '张三丰'
name2 = "张无忌"
print(type(name1))
print(type(name2))
name3 = """XX大学"""
name4 = '''学苑路1号'''
print(type(name3))
print(type(name4))
2.字符串下标
语法规则:
    2.1 下标起始值 0
    2.2 最后一个值 len()-1 
    2.3 使用[]+数字形式
hello = "helloworld"
print(hello[0])
print(hello[len(hello)-1])
3.切片
语法规则:
    3.1 变量名[起始下标:结束下标:步长]
    3.2 左闭右开   起始下标<=  x <结束下标
    3.3 起始下标默认为0 可以不写
    3.4 结束下标默认为最后一个下标值  可以不写
    3.5 步长默认为1 可以不写
    3.6 步长为负数则从右往左
    3.7 起始下标或结束下标为负数则 从右往左数
   
address = "XXXX大学学苑路101号"
# 3.1 大学
print(address[4:6])

# 3.2 XXXX大学
print(address[:6])

# 3.3 学苑路101号
print(address[6:])

# 3.4 XX大学路1号
print(address[::2])

# 3.5 号101路苑学学大XXXX
print(address[::-1])

# 3.6 101
print(address[-4:-1])

2.字符串常用操作

# 1.查找

# 1.1 find() 查找子串是否存在,返回下标值否则返回-1

address = "XXXX大学学苑路101号"
print("1.1 find()函数:",address.find("路"))
print("1.1 find()函数:",address.find("张"))


# 1.2 index() 查找子串是否存在,返回下标值否则报错

print("1.2 index()函数:",address.index("路"))
# print("1.2 index()函数:",address.index("张"))


# 1.3 rfind() 从右侧查找和find函数一致

print("1.3 rfind()函数:",address.rfind("1"))


# 1.4 rindex() 从右侧查找和index函数一致

print("1.4 rindex()函数:",address.rindex("1"))


# 1.5 count函数 统计子串的次数,若存在返回数字,若不存在返回0

print("1.5 count()函数:",address.count("1"))
print("1.5 count()函数:",address.count("张"))



## 2.修改


2.1 replace 替换
字符串序列.replace(旧子串,新子串,替换次数)
注意事项:
    1.替换生成新的字符串返回
    2.替换次数根据实际数量决定


address1 = address.replace("1","2",4)
print("2.1 replace函数:",address1)
print("2.1 replace函数:",address)



2.2 split 分割字符串
字符串序列.split(分割字符,num)
num表示分割字符出现的次数
语法规则:
    1.字符串分割后返回值为list
    2.分隔符不再返回值之列


hello = "HelloWorld"
print("2.2 split()函数:",hello.split("l",4))



2.3 join 合并字符串
字符或子串.join(多字符串组成的序列)


listJoin = ['张三丰','张无忌','张学友','张杰']
print("2.3 join函数:","/".join(listJoin))
tupleJoin = ("我","是",'18','岁')
print("2.3 join函数:","".join(tupleJoin))


2.4 capitalize() 字符串字符转成首字母大写

hello = "hello world"
print("2.4 capitalize():",hello.capitalize())



2.5 title()字符串每个单词首字母大写

hello = "hello world"
print("2.5 title():",hello.title())



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


hello = "HELLO WORLD"
print("2.6  lower():",hello.lower())



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

hello = "hello world"
print("2.7 upper():",hello.upper())



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


hello = "    helloworld"
print("2.8 lstrip()函数:%s"%hello.lstrip())



2.9 rstrip() 删除字符串右侧空白字符

hello = "helloworld        "
print("2.9 rstrip()函数:%s"%hello.rstrip(),end="#\n")



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


hello = "     helloworld    "
print("2.10 strip():%s"%hello.strip(),end="#\n")


2.11 ljust() 返回字符串左对齐

hello = "hello"
print("2.11 ljust()函数:%s"%hello.ljust(10,"#"))



2.12 rjust() 返回字符串右对齐

print("2.12 rjust()函数:%s"%hello.rjust(10,"#"))



2.13 center() 居中对齐

print("2.13 center():%s"%hello.center(10,"#"))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值