python基础之字符串常用方法
- str常用命令:
字符操作:.capitalize() .upper() .lower() .title() .swapcase()
判断:.startwith() .endwith() .isalnum() .isalpha() .isdigit()
统计:.count() len()
索引:.find() .index()
结构:.format() .strip() .lstrip() .rstrip() .split() .replace .center() .expandtabs()
循环:for
1,首字母大写:
s.capitalize()
- s1 = 'python is good'
- s2 = s1.capitalize()
- print(s2)
- 2,全部大写:
s.upper()
- s1 = 'python is good'
- s2 = s1.upper()
- print(s2)
- 3,全部小写
s.lower()
- s1 = 'Python iS good'
- s2 = s1.lower()
- print(s2)
- result:python is good
4,大小写翻转:
s.swapcase()
- s1 = 'Python iS good'
- s2 = s1.swapcase()
- print(s2)
- result:pYTHON Is GOOD
5,每个分隔的单词首字母大写:
s.title()
- s1 = 'Python iS good'
- s2 = s1.title()
- print(s2)
- result:Python Is Good
6,字符串居中:
s.center(20)--20代表输出宽度
- s1 = 'Python iS good'
- s2 = s1.center(20)
- print(s2)
- s.center(20,'-')--符号代表填充内容
- s1 = 'Python iS good'
- s2 = s1.center(20,'-')
- print(s2)
- 7,自动填充,当字符串中有\t(tab键),前面不足8位的补充到8位,不足16未补充到16位:
s.expandtabs()
8,字符串长度:
len(s)--长度
- s1 = 'Python iS good'
- s2 = len(s1)
- print(s2)
- result:14
9,判断字符串以什么开头/结尾:
s.startwith('e',start='',end='')--e表示查找对象,start/end切片定位,返回true/faulse
- s1 = 'Python iS good'
- s2 = s1.startswith('o')
- print(s2)
- result:False
- s1 = 'Python iS good'
- s2 = s1.startswith('P')
- print(s2)
- result:true
- s1 = 'Python iS good'
- s2 = s1.startswith('P',2,5)
- print(s2)
- result:False
s.endwith('')--同理
10.根据元素找索引:
s.find('')--返回元素位置索引,未找到返回-1.
- s1 = 'Python iS good'
- s2 = s1.find('S')
- print(s2)
- result:8
- s1 = 'Python iS good'
- s2 = s1.find('w')
- print(s2)
- result:-1
s.index('')--同理,未找到报错
- s1 = 'Python iS good'
- s2 = s1.index('w')
- print(s2)
- result:ValueError: substring not found
- s1 = 'Python iS good'
- s2 = s1.index('S')
- print(s2)
- result:8
11,去掉空格:常用于有用户输入的地方
s.strip()--去掉前后空格
- s1 = ' Python iS good '
- s2 = s1.strip()
- print(s2)
- result:Python iS good
s.strip('%')--%表示要去掉的所有元素,可多个元素-%q,也可有空格- %q- s.lstrip()--去掉前端空格
- s1 = ' Python iS good '
- s2 = s1.lstrip()
- print(s2)
- result:Python iS good
s.rstrip()--去掉尾部空格
- s1 = ' Python iS good '
- s2 = s1.rstrip()
- print(s2)
- result: Python iS good
*strip的坑:--前后相关字段都去掉了
- s = 'ab12aaab123a33b'
- print(s.strip('ab'))
- result:12aaab123a33
12,统计元素的个数:
s.count('e')--返回e元素个数
- s1 = ' Python iS good '
- s2 = s1.count('o')
- print(s2)
- result:3
- #按索引切片统计
- s1 = ' Python iS good '
- s2 = s1.count('o',6,14)
- print(s2)
- result:2
13,分割元素:
s.split()--默认以空格分隔,返回元素列表
- s1 = ' Python iS good '
- s2 = s1.split()
- print(s2)
- result:['Python', 'iS', 'good']
s.split(',')--以,分隔
- s1 = ' Python iS good,english is good too '
- s2 = s1.split(',')
- print(s2)
- result:[' Python iS good', 'english is good too ']
14,格式化输出:format三种方法
s=‘我叫{},今年{}'.format('wang','18')--{}表示占位符,等同于%s,format依次补充内容
- s1 = '我叫{},今年{}'.format('wang','')
- print(s1)
- result:我叫wang,今年18
s=‘我叫{0},今年{1},再说一下我叫{0}'.format('wang','18')--使用索引标识输出位
- s1 = '我叫{0},今年{1},再说一下我叫{0}'.format('wang',''
print(s1
- result:我叫wang,今年18,再说一下我叫wang
s=‘我叫{name},今年{age}'.format(name='wang',age='18')--使用变量标识输出内容
- s1 = '我叫{name},今年{age}'.format(name='wang',age='')
- print(s1)
- result:我叫wang,今年18
15,元素替换:
s.replace(old,new,count=None)--old-原元素,new-新元素,count-替换个数,默认替换全部
- s1 = 'python is good'
- s2 = s1.replace('o','t')
- print(s2)
- result:pythtn is gttd
- s1 = 'python is good'
- s2 = s1.replace('o','t',2)
- print(s2)
- result:pythtn is gtod
16,判断:
s.isalnum()--由字母与数字组成,返回true/false
- s1 = ''
- s2 = s1.isalnum()
- print(s2)
- result:True
- s1 = 'aa133'
- s2 = s1.isalnum()
- print(s2)
- result:True
s.isalpha()--由字母组成
- s1 = 'aassss'
- s2 = s1.isalpha()
- print(s2)
- result:True
s.isdigit()--由数字组成
- s1 = 'aassss'
- s2 = s1.isdigit()
- print(s2)
- result:False
17,循环:
for i in s:
print(i)--依次遍历
- s1 = 'python is good'
- for i in s1:
- print(i)
- result:
p
y
t
h
o
n
..省略.
python基础之字符串常用方法的更多相关文章
- python基础3 字符串常用方法
一. 基础数据类型 总览 int:用于计算,计数,运算等. 1,2,3,100...... str:'这些内容[]' 用户少量数据的存储,便于操作. bool: True, False,两种状态 ...
- python基础(字符串常用方法)
字符串不常用方法: 字符串常用的方法: #看源代码 按住ctrl点击方法名 用户注册的小程序 import datetimeusers = []passwds = []for i in range(3 ...
- Python基础数据类型-字符串(string)
Python基础数据类型-字符串(string) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版 ...
- Python基础(二) —— 字符串、列表、字典等常用操作
一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 二.三元运算 result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为 ...
- python基础类型—字符串
字符串str 用引号引起开的就是字符串(单引号,双引号,多引号) 1.字符串的索引与切片. 索引即下标,就是字符串组成的元素从第一个开始,初始索引为0以此类推. a = 'ABCDEFGHIJK' p ...
- python基础、字符串和if条件语句,while循环,跳出循环、结束循环
一:Python基础 1.文件后缀名: .py 2.Python2中读中文要在文件头写: -*-coding:utf8-*- 3.input用法 n为变量,代指某一变化的值 n = inpu ...
- python学习之字符串常用方法和格式化字符串
Python中的字符串同样适用标准的序列操作(索引,分片,乘法,成员判断,求长度,取最小值和最大值),但因为字符串是不可变的,因此字符串不支持分片赋值. s='http://www.baidu.com ...
- Python基础__字符串拼接、格式化输出与复制
上一节介绍了序列的一些基本操作类型,这一节针对字符串的拼接.格式化输出以及复制的等做做详细介绍.一. 字符串的拼接 a = 'I', b = 'love', c = 'Python'. 我们的目的是: ...
- Python基础二字符串和变量
了解一下Python中的字符串和变量,和Java,c还是有点区别的,别的不多说,上今天学习的代码 Python中没有自增自减这一项,在转义字符那一块,\n,\r\n都是表示回车,但是对于不同的操作系统 ...
随机推荐
- loj2589 「NOIP2009」Hankson 的趣味题
对于质因数分解理解还不到位. 此题可知$lcm$是$x$的倍数,$x$是$lcm$的约数,只要在$lcm$的分解质因数里对每一个质因子讨论种数即可. 具体来说,对于$lcm$的一个质因子$p$,讨论$ ...
- HTML左边盒子固定,右边盒子自适应
html: <div class="box1"> <div class="divA">DIVA</div> <div ...
- 2017 网易游戏互娱游戏研发4.21(offer)
网易游戏互娱(offer) 去年这个时候就参加过网易游戏的实习生招聘,到今年总共收到了4次拒信.不过这次运气好,终于get了最想要的offer.去年实习生互娱笔试挂,秋招笔试挂,今年春招互娱投了连笔试 ...
- C# JSON的序列化与反序列化
需要添加引用:System.ServiceModel.Web 和 System.Runtime.Serialization,然后使用Using: using System.Runtime.Serial ...
- 1223 drf引入以及restful规范
目录 前后台的数据交互 drf 知识点概括 1. 框架安装 2. 接口 2.1 什么是接口 2.2 接口文档 2.3 接口工具的使用 2.4 restful接口规范 debug的使用 前后台的数据交互 ...
- 模意义下的FFT算法
//写在前面 单就FFT算法来说的话,下面只给出个人认为比较重要的推导,详细的介绍可参考 FFT算法学习笔记 令v[n]是长度为2N的实序列,V[k]表示该实序列的2N点DFT.定义两个长度为N的实序 ...
- Visual Studio下__cplusplus宏为199711L的问题
Visual Studio下__cplusplus宏为199711L的问题 / Zc:__ cplusplus(启用更新的__cplusplus宏) 该/ ZC:__ CPLUSPLUS编译器选项使_ ...
- UVA 10900 So do you want to be a 2^n-aire?
#include<bits/stdc++.h> #include<stdio.h> #include<iostream> #include<cmath> ...
- Python黑科技:赋值技巧
一个变量一个值(正常赋值) v = 1 ''' # 结果,v: 1 ''' 多个变量一个值(连续赋值) x = y = z = 0 ''' # 结果,x: 0, y: 0, z: 0 ''' # 注意 ...
- Java字符串的替换(replace()、replaceFirst()和replaceAll())
在 Java 中,String 类提供了 3 种字符串替换方法,分别是 replace().replaceFirst() 和 replaceAll(),本文将详细介绍它们的使用方法. replace( ...