Python中的字符串处理

1.字符串的定义

a = 'hello'
b = "westos"
c = 'let\'s go'
d = "let's go"
e = """
        用户管理系统
        1.添加用户
        2.删除用户
        3.显示用户
"""
print(a)
print(b)
print(c)
print(d)
print(e)
print(type(e))

输出结果:
在这里插入图片描述

2.字符串的索引

s = 'hello'
print(s[0])		//输出第一个字符h
print(s[1])		//输出第二个字符e

在这里插入图片描述

3.字符串的切片

s = 'hello'
print(s[:3])		//输出前3个字符
print(s[0:3])		//输出前3个字符
print(s[0:4:2]) #s[start:end:step] 从start开始,到end-1结束,步长为2
print(s[:])			//输出整个字符串
print(s[::-1])		//反向输出字符串
print(s[1:])		//输出第2个到结尾的字符
print(s[:-1])		//从开头输出到倒数第1个为止
print(s[1])			//输出第2个字符
print(s[-1])		//输出倒数第1个字符

在这里插入图片描述
判断回文数: 用c语言想必大家都写过,下面用Python写,相比之下就简单多了。

num = input('Num:')
if num == num[::-1]:
    print('回文数')
else:
    print('不是')

4.字符串的重复

s = 'hello'
print(s * 5)

在这里插入图片描述

5.字符串的连接

print('hello' + 'world')

在这里插入图片描述

6.字符串的成员操作符号

s = 'hello'
print('h' in s)
print('q' in s)

在这里插入图片描述

7.转化为小写/大写输出

print('hello'.upper())	//转化为大写输出
print('HELLO'.lower())	//转化为小写输出

8.判断大小写和数字

print('123'.isdigit()) 		//判断字符串是否为数字
print('Hello'.istitle())	//判断字符串是否为标题(首字母大写)
print('hello'.isupper())	//判断是否为大写
print('HELLO'.islower())	//判断是否为小写
print('123'.isalpha())		//判断是否为字母

9.判断开头结尾

url1 = 'http://172.25.254.250/index.html'
url2 = 'ftp:///172.25.254.250/pub'
url3 = 'file://mnt'
filename = 'login.php'
if url3.startswith('http://'):	
    print("ok")
elif filename.endswith('.php'):
    print("yes")        

10.去除字符串中特定的字符

s.strip()		//去除s字符串空格,回车,制表符
s.lstrip()		//去除左边的空格
s.rstrip()		//去除右边的空格
s.strip('a')	//去掉里边字符串里边的a
s.lstrip('a')	//去掉左边字符串里边的a
s.rstrip('a')	//去掉右边字符串里边的a

11.查找

s.find('hello')	//找到s中的子串并返回最小索引
s.rfind('hello')	//找到s中的子串并返回最大索引

12.替换

s.replace('hello','westos') 	//把hello替换成westos

13.字符串的对齐

print('学生管理系统'.center(20))		//总共打印20个字符,学生管理系统放在最中间
print('学生管理系统'.center(20,'*'))	//总共打印20个字符,学生管理系统放在最中间,其他的使用*填充
print('学生管理系统'.ljust(20,'*'))	//总共打印20个字符,学生管理系统放在最左边,其他使用*代替
print('学生管理系统'.rjust(20,'*'))	//总共打印20个字符,学生管理系统放在最右边,其他使用*代替

13.计数

print('hello'.count('l'))	//统计hello中l的个数

14.分割

s='172.25.254.250'
print(s.split('.'))		//将s字符串以'.'分割成列表
print(s.split('.')[::-1])	//以'.'分割倒叙输出

15.连接

date=['2019','06','26']
print('-'.join(date))		//字符串连接,以'.'连接
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python有多种字符串处理方法。下面是几种常见的字符串处理方法: 1. 去掉头尾空格:可以使用strip()方法。strip()方法用于去除字符串开头和结尾的空白符,包括换行、回车、制表符和空格。例如,对于字符串" hello python ",使用st.strip()将去除头尾的空格,得到"hello python"。 2. 去掉头部空格:可以使用lstrip()方法。lstrip()方法用于去除字符串开头的字符和空白符。例如,对于字符串" hello python ",使用st.lstrip()将去除开头的空格,得到"hello python"。 3. 去掉尾部空格:可以使用rstrip()方法。rstrip()方法用于去除字符串结尾的字符和空白符。例如,对于字符串" hello python ",使用st.rstrip()将去除结尾的空格,得到"hello python"。 4. 字符串替换:可以使用replace()方法。replace()方法可以将字符串的旧字符串替换为新字符串。如果指定第三个参数,可以限制替换的次数。例如,对于字符串" hello python ",使用st.replace('python', 'word')将把"python"替换为"word",得到"hello word"。 5. 字符串切片:可以使用split()方法。split()方法可以按照指定的分隔符将字符串分割成多个子字符串,并返回一个子字符串列表。例如,对于字符串"What's your name?",可以使用st.split(' ')将其切分成多个子字符串,得到["What's", "your", "name?"]。 这些是一些常见的字符串处理方法,可以帮助你在Python字符串进行各种操作和处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [python教程:15种字符串操作方法](https://blog.csdn.net/qdPython/article/details/124063345)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Python——字符串处理](https://blog.csdn.net/JD20200906/article/details/125872342)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值