python---字符串详解
1.isupper判断字符串是否全部都是大写
#eg:
str1 = 'Hello,world'
str2 = 'HELLO,WORLD'
res = str2.isupper()
print(res)
True
2.isalnum判断字符串里是否是数字或字母不能有其他字符
#eg:
str1 = '11122333aaa'
res = str1.isalnum()
print(res)
True
3.isdigit判断字符串里面是否是整型
#eg:
str1 = '123'
print(str1.isdigit())
True
#eg:
str1 = 'Hello,world'
print(str1.isdigit())
False
4.upper()方法把字符串全部变成大写
#eg:
str1 = 'Hello,world'
print(str1.upper())
HELLO,WORLD
5.islower判断字符串是否为小写
#eg:
str1 = 'Hello,world'
str1.islower()
False
6.startswith判断字符串开头是否为He
str1 = 'Hello,world'
print(str1.startswith('He'))
True
7.endswith判断字符串结尾是否为ld
#eg:
str1 = 'Hello,world'
print(str1.endswith('ld'))
True
8.index取字符串o的下标,如果没有这个字符会报错
rindex是从右往左检索但标记的时候依然是从左往右标记
#eg:
str1 = 'Hello,world'
print(str1.index('o'))
9.find取字符串o的下标,如果没有这个字符返回-1
str1 = 'Hello,world'
print(str1.find('o'))
10.isalpha判断字符串里是否全部都是英文
#eg:
str1 = 'Hello,world'
print(str1.isalpha())
False
#eg:
str1 = 'Helloworld'
print(str1.isalpha())
True
11.count统计字符串里l字符的个数
#eg:
str1 = 'Hello,world'
print(str1.count('l'))
3
12.istitle判断是否是抬头
#eg:
str1 = 'Hello,world'
print(str1.istitle())
False
#eg:
str1 = 'Hello ,World'
print(str1.istitle())
True
13.把一个字符串变成抬头
#eg:
str1 = 'Hello,world'
print(str1.title())
Hello,World
14.isspace判断字符串是否是纯空格
#eg:
str1 = ' '
print(str1.isspace())
True
#eg:
str1 = ''
print(str1.isspace())
False
#需要必须掌握的
15.replace替换字符串o成sb,并且只替换1次
#eg:
str1 = 'Hello,world'
res = str1.replace('o','sb',1)
print(res)
Hellsb,world
#eg:
str1 = 'Hello,world'
res = str1.replace('o','sb',2)
print(res)
Hellsb,wsbrld
#eg:
str1 = 'Hello,world'
res = str1.replace('o','sb')
print(res)
Hellsb,wsbrld
16.把一个可迭代对象(列表,元组,集合,字典,字符串)变成字符串
#eg:
res = ''.join('aaa')
print(res)
print(type(res))
aaa
eg:
res='a'.join(['1','2'])
print(res)
1a2
17.把一个字符串从左往右切分变成列表(.代表切分点,1代表切分1次)
#eg:
str1 = '192.168.160.132'
res = str1.split('.',1)
print(res)
['192', '168.160.132']
#eg:
str1 = '192.168.160.132'
res = str1.split('.',2)
print(res)
['192', '168', '160.132']
#eg:
str1 = '192.168.160.132'
res = str1.split('.')
print(res)
['192', '168', '160', '132']
18.把一个字符串从右往左切分变成列表(.代表切分点,1代表切分1次)
#eg:
str1 = '192.168.160.132'
res = str1.rsplit('.',1)
print(res)
['192.168.160', '132']
19.去除字符串左右两边指定的字符
#eg:
str1 = '++++++Hello,World====='
res = str1.strip('=')
print(res.strip('+'))
Hello,world
20.去除字符串右边指定的字符
#eg:
str1 = '++++++Hello,World====='
res = str1.rstrip('=')
print(res)
++++++Hello,world
21.去除字符串左边指定的字符
#eg:
str1 = '++++++Hello,World====='
res = str1.lstrip('+')
print(res)
Hello,world=====
22.format将字符串格式化,可以有以下3种格式
#eg:
str1 = 'my name is {},my age is {}'
res = str1.format('吉喆', '23')
print(res)
my name is 吉喆,my age is 23
#eg:
str1 = 'my name is {1},my age is {0}'
res = str1.format('23', '李凯')
print(res)
my name is 李凯,my age is 23
#eg:
str1 = 'my name is {name},my age is {age}'
res = str1.format(name='李凯', age='23')
print(res)
my name is 李凯,my age is 23
23.%s,%d,%f可以格式化字符串
%d只可以接收整数,%s可以接收数字也可以接收字符串%f接受浮点型
#eg:
str1 = 'my name is %s, my age is %d'
res = str1 % ('吉喆', 23)
print(res)
my name is 吉喆,my age is 23
#eg:
str1 = 'my name is %s, my age is %s'
res = str1 % ('吉喆', 23)
print(res)
my name is 吉喆,my age is 23
#eg:
str1 = 'my name is %s, my age is %d'
res = str1 % ('吉喆', ‘23’)
print(res)
错误
24.利用索引或者下标取值,超出范围报错从前往后是0 1 2 3......
从后往前是-1 -2 -3 -4 .......
#eg:
str1 = 'Hello,World'
print(str1[-1])
d
#eg:
str1 = 'Hello,World'
print(str1[1])
e
25.字符串的拼接
#eg:
print(str1[4]+str1[5])
o,
26.切片
#eg:
str1 = 'Hello,World'
res = str1[2:5]#正向切片顾头不顾尾
print(res)
llo
#eg:
str1 = 'Hello,World'
res = str1[-4:-1]#反向也是顾头不顾尾
print(res)
orl
#eg:
str1 = 'Hello,World'
res = str1[:3]#索引为3往右的字符不要了(包括下标为3的字符)
print(res)
Hel
#eg:
str1 = 'Hello,World'
res = str1[3:]#索引为3往左的字符不要了(不包括下标为3的字符)
# print(res)
lo,world
#eg:
str1 = 'Hello,World'
res = str1[::2]#步长为2,隔一个字符取一个字符
print(res)
Hlowrd
27.三引号和双引号和单引号任意切换
#eg:
str1 = '''
"what's your name????"
'''
print(str1)
"what's your name????"
有单引号可以用双引号括,有双引号可以用三引号括,有三引号可以用单引号括
python---字符串详解相关推荐
- C# json解析字符串总是多出双引号_Python小白必备知识:Python字符串详解
若干个字符的集合就是一个字符串(String).Python 中的字符串必须由双引号" "或者单引号' '包围,具体格式为: "字符串内容" '字符串内容' 字 ...
- Python字符串详解(包含长字符串和原始字符串)
若干个字符的集合就是一个字符串(String).Python 中的字符串必须由双引号" "或者单引号' '包围,具体格式为: "字符串内容" '字符串内容' 字 ...
- Python字符串详解
若干个字符的集合就是一个字符串(String).Python 中的字符串必须由双引号" "或者单引号' '包围,具体格式为: "字符串内容" '字符串内容' 字 ...
- python 字符串详解(附案例)
目录 什么是python字符串 如何表示一个字符串 字符串的访问 print() 索引 for循环 打印字符--end() 切片split 范围选择符 [ n : m ] 字符串长度 使用len()函 ...
- Python字符串详解(2)
字符串处理函数 一些以函数形式提供的字符串处理功能 1.len(x) 求取字符串的长度,输出为字符串x的长度 示例代码如下: print(len("一二三四五六七89")) 运行截 ...
- python 字符串详解
Python有五大数据类型,分别是:使用type内置函数可以查看数据类型 1:str(字符串) 2:int(整数) 3:float(浮点数) 3:list(列表) 4:dict(字 ...
- python 字符串替换_Python基础教程,第四讲,字符串详解
本节课主要和大家一起学习一下Python中的字符串操作,对字符串的操作在开发工作中的使用频率比较高,所以单独作为一课来讲. 学完此次课程,我能做什么? 学完本次课程后,我们将学会如何创建字符串,以及如 ...
- python字符串是什么_python字符串详解
字符串详解 在python中引号引起来的就是字符串 字符串是用来存储少量数据 索引 索引(下标) 通过索引可以精确的定位到某个元素 name = "meat" meat 每一个字母 ...
- Python基础语法day02字符串详解和列表
今天是python基础语法入门第二天,大概总共会有四天左右.四天后就是对于python的numpy库的详细文章.那话不多说,我们开始. 目录 字符串详解 字符串独有功能 检测头尾字符 判断输入结果是否 ...
- python中的字符串详解(附实现文字跑马灯效果)
python中的字符串详解 1.什么是字符串 相信大家对字符串应该是再熟悉不过了吧,因为日常生活中的一句话,或是路边的一则广告,其实它都相当于一个字符串,而顾名思义,字符串也就是一串串的由零个或多 ...
最新文章
- 遗传算法与C++实现
- python算法攻略_算法基础及python实现笔记一(堆和DFS)
- 真实的布兰妮,有点壮
- java socket 全双工_java socket实现全双工通信
- 前端的UI设计与交互之反馈示篇
- BGP劫持信息有望通过基于推特的系统放送
- HMM隐马尔可夫模型-通俗易懂
- linux 默认网关自动消失,route add default gw 添加默认网关,重起后消失
- Winform实现微信功能
- WEB前端知识大整合之JS表单验证
- 编译原理 | 实验四 | 逆波兰式
- 一张图玩转 XMind 思维导图软件
- win10下的VS2017安装MPI
- 读文章笔记(八):多模态情感分析数据集整理
- 音视频编解码技术之视频编码基本概念介绍
- PE格式详细讲解2 - 系统篇02
- (二)LINGO入门---使用篇
- 腾讯云代理商:共青城市与“腾讯云”举行战略合作协议远程签约仪式
- 【MySQL】之重置数据库主键 ID
- 为什么黑帽子从不用鼠标,一直在敲键盘?看完长见识了!