字符串

print('chenxi'*8)

  测试

D:\python\python.exe D:/untitled/dir/for.py
chenxichenxichenxichenxichenxichenxichenxichenxi Process finished with exit code 0

  字符串切片打印处理

print('chenxi'[2:])

  测试

D:\python\python.exe D:/untitled/dir/for.py
enxi

  判断字符串里有没有包含对应得字符

print('x'in 'chenxi')
print('f'in 'chenxi')

  测试

True
False

  字符串拼接,不建议使用这种方法

a='cx'
b='zrd'
c=a+b
print(c)

  测试

D:\python\python.exe D:/untitled/dir/for.py
cxzrd Process finished with exit code 0

  字符串拼接

a='cx'
b='zrd'
c=''.join([a,b])
print(c)

  测试

D:\python\python.exe D:/untitled/dir/for.py
cxzrd Process finished with exit code 0

  字符串拼接

a='cx'
b='zrd'
bb='haha'
c='-----'.join([a,b,bb])
print(c)

  测试

D:\python\python.exe D:/untitled/dir/for.py
cx-----zrd-----haha

  字符串之统计关键字个数

dis = 'hebei tianjing guanzhou'
print(dis.count('i'))

  测试

D:\python\python.exe D:/untitled/dir/for.py
3

  字符串之修改首字母为大写

dis = 'hebei tianjing guanzhou'
print(dis.capitalize())

  测试

D:\python\python.exe D:/untitled/dir/for.py
Hebei tianjing guanzhou

  一共打印50字符,不够用指定字符去补,原先字符串内容居中

dis = 'hebei tianjing guanzhou'
print(dis.center(50,'#'))

  测试

D:\python\python.exe D:/untitled/dir/for.py
#############hebei tianjing guanzhou############## Process finished with exit code 0

  判断字符串是不是以什么结尾的

dis = 'hebei tianjing guanzhou'
print(dis.endswith('u'))
print(dis.endswith('i'))

  测试

D:\python\python.exe D:/untitled/dir/for.py
True
False Process finished with exit code 0

  判断字符串是不是以什么开头的

dis = 'hebei tianjing guanzhou'
print(dis.startswith('he'))

  测试

D:\python\python.exe D:/untitled/dir/for.py
True Process finished with exit code 0

  修改字符串里tab的默认空格数量\t 表示tab键

dis = 'he\tbei tianjing guanzhou'
print(dis.expandtabs(tabsize=10))

  测试

D:\python\python.exe D:/untitled/dir/for.py
he bei tianjing guanzhou Process finished with exit code 0

  找到字符串里第一个元素定返回索引值

dis = 'hebei tianjing guanzhou'
print(dis.find('a'))

  测试

D:\python\python.exe D:/untitled/dir/for.py
8

  将字符串变量赋值打印

dis = 'hebei tianjing guanzhou {name}'
print(dis.format(name='zrd'))

  测试

D:\python\python.exe D:/untitled/dir/for.py
hebei tianjing guanzhou zrd Process finished with exit code 0

  以字典方式批量给字符串赋值

dis = 'hebei tianjing guanzhou {name} ll {age}'
print(dis.format_map({'name':'zrd','age':22}))

  测试

D:\python\python.exe D:/untitled/dir/for.py
hebei tianjing guanzhou zrd ll 22 Process finished with exit code 0

  查字符串里关键字并返回索引值,字符串里没有关键字报错

dis = 'hebei tianjing guanzhou {name} ll {age}'
print(dis.index('i'))
print(dis.index('d'))

 测试

D:\python\python.exe D:/untitled/dir/for.py
Traceback (most recent call last):
File "D:/untitled/dir/for.py", line 170, in <module>
print(dis.index('d'))
ValueError: substring not found
4

  判断字符串是否包含特殊字符

print('test564'.isalnum() )
print('tygc*'.isalnum())
print('reswd'.isalnum())
print('7890'.isalnum())
print('宋氏家族'.isalnum())

  测试

D:\python\python.exe D:/untitled/dir/for.py
True
False
True
True
True

  判断字符串像不像十进制的数字

print("3455".isdecimal())
print("程序".isdecimal())
print("AF09".isdecimal())
print("hevb".isdecimal())

  测试

D:\python\python.exe D:/untitled/dir/for.py
True
False
False
False

  判断字符串是否包含一个非法字符

print("dtfghvh".isidentifier())
print("1233tyghgvh".isidentifier()) #非法字符

  测试

D:\python\python.exe D:/untitled/dir/for.py
True
False

  判断字符是不是空格

print(' '.isspace())

  测试

D:\python\python.exe D:/untitled/dir/for.py
True Process finished with exit code 0

  判断字符串是不是标题格式

print('My Ch'.istitle())
print('My ch'.istitle())

  测试

True
False

  字符串里所有大写改成小写

print('My Ch'.lower())

  测试

D:\python\python.exe D:/untitled/dir/for.py
my ch Process finished with exit code 0

  字符串所有小写该大写

print('My Ch'.upper())

  测试

D:\python\python.exe D:/untitled/dir/for.py
MY CH Process finished with exit code 0

  字符串中大小写翻转

print('My Ch'.swapcase())

  测试

D:\python\python.exe D:/untitled/dir/for.py
mY cH Process finished with exit code 0

  在字符串后面以特定字符补够特定数量

print('My Ch'.ljust(50,'*'))

  测试

D:\python\python.exe D:/untitled/dir/for.py
My Ch********************************************* Process finished with exit code 0

  在字符串前面以特定字符补够特定数量

print('My Ch'.rjust(50,'*'))

  测试

D:\python\python.exe D:/untitled/dir/for.py
*********************************************My Ch Process finished with exit code 0

  将字符串前后空格去掉

print('       My Ch'.strip())

  测试

D:\python\python.exe D:/untitled/dir/for.py
My Ch Process finished with exit code 0

  字符串内容替换

print('chenxi ffff'.replace('ffff','zrd'))

  测试

D:\python\python.exe D:/untitled/dir/for.py
chenxi zrd Process finished with exit code 0

  字符串内容替换的次数控制

print('chenxi ffff'.replace('ffff','zrd'))
print('chenxi ffff tygdf'.replace('ff','zrd'))
print('chenxi ffff tygdf'.replace('ffff','zrd',1))

  测试

D:\python\python.exe D:/untitled/dir/for.py
chenxi zrd
chenxi zrdzrd tygdf
chenxi zrd tygdf Process finished with exit code 0

  查字符串最后一个关键字在第几个索引

print('chenxi cx xlc'.rfind('x'))

 测试

D:\python\python.exe D:/untitled/dir/for.py
10 Process finished with exit code 0

  将字符串以空格为分隔符,分成列表

print('chenxi cx xlc'.split(' '))

  测试

D:\python\python.exe D:/untitled/dir/for.py
['chenxi', 'cx', 'xlc'] Process finished with exit code 0

  

python 基础之字符串方法的更多相关文章

  1. 【python基础】字符串方法汇总

    一.声明 0-多个字符组成的有序序列; 二.特点 1. 字符串是一个不可变的数据类型 2.字符串是有序的 三.索引 下标:'abcde' 1.从左到右, 0, 1,2, ... 2.从右到左, 索引值 ...

  2. python基础 while 字符串方法 运算符

    一.while 1.while 死循环 f=True while f: print(1) print(2) 2.while 活循环 ①.正序 count = 1 while count <= 5 ...

  3. Python基础数据类型-字符串(string)

    Python基础数据类型-字符串(string) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版 ...

  4. Python中的字符串方法

    Python中的字符串方法 字符串类即str提供了许多有用的方法来操纵字符串.具体来说,我们将讨论如下的方法. 搜索字符串内的子字符串. 测试字符串. 格式字符串. 转换字符串. 回顾前面的章节,方法 ...

  5. Python基础(二) —— 字符串、列表、字典等常用操作

    一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 二.三元运算 result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为 ...

  6. python基础知识——字符串详解

    大多数人学习的第一门编程语言是C/C++,个人觉得C/C++也许是小白入门的最合适的语言,但是必须承认C/C++确实有的地方难以理解,初学者如果没有正确理解,就可能会在使用指针等变量时候变得越来越困惑 ...

  7. Python 入门之Python基础数据类型及其方法

    Python 入门之Python基础数据类型 1. 整型:int 用于计算,用于比较 (在赋值的时候先执行等号右边的内容) 1.1 整数的加 a = 10 b = 20 print(a + b) 结果 ...

  8. Python基础__字符串拼接、格式化输出与复制

    上一节介绍了序列的一些基本操作类型,这一节针对字符串的拼接.格式化输出以及复制的等做做详细介绍.一. 字符串的拼接 a = 'I', b = 'love', c = 'Python'. 我们的目的是: ...

  9. python基础、字符串和if条件语句,while循环,跳出循环、结束循环

    一:Python基础 1.文件后缀名: .py 2.Python2中读中文要在文件头写: -*-coding:utf8-*- 3.input用法      n为变量,代指某一变化的值 n = inpu ...

随机推荐

  1. Spring - Spring 常用注解

    概述 简单整理一些 Spring 的注解 这个算是一个 水一波 类型的整理 内容不全 分类可能有的地方不会太符合逻辑 而且时间也不太充裕 先把自己想写的写下来, 然后随缘整理吧 约定 版本 Sprin ...

  2. 豆瓣工程师为你解答关于 Python3 编程方面的问题

    Python是如此活跃的一种语言,几乎伴随互联网的发生而创立,又伴随互联网的极速发展而繁荣.使用Python会遇到这样的问题:什么时候该用多进程?怎样提高代码执行效率?Flask为什么流行?学习Pyt ...

  3. angular2 单元测试 路由相关

    第一步:在html模板中,写路由链接,并保证有路由出口 第二步:写自定义的路由指令和路由出口组件,因为在单元测试中不需要引入真实的路由,此处我们用虚拟的代替即可. 第三步:将自定义的虚拟路由指令和路由 ...

  4. GitBook的账号注册和安装使用(一)

    1.GitBook账号注册 GitBook原注册地址  https://legacy.gitbook.com/join 改为: https://www.gitbook.com/ (1)注册账号:htt ...

  5. mybatis 源码分析--日志分析

    1. MyBatis 没有提供日志实现,需要接入第三方的日志组件,但是第三方的日志组件都各自的Log级别,而不相同 实现方式:适配器模式   Slf4jImpl 2. 自动扫描日志实现,并且第三方日志 ...

  6. sockfd_to_family函数

    #include <sys/socket.h> #include <netinet/in.h> #define SA struct sockaddr int sockfd_to ...

  7. 爬虫,工具 - Splash

    What is it? Splash is a javascript rendering service. It's a lightweight web browser with an HTTP AP ...

  8. SpringBoot2.x过后static下的静态资源无法访问

    @Configuration public class CorsConfig extends WebMvcConfigurationSupport {/** * 添加静态资源文件,外部可以直接访问地址 ...

  9. JavaSE复习~运算符与表达式

    运算符 运算符:进行特定操作的符号 表达式:用运算符进行操作的式子 算术运算符 首先是加减乘除:+.-.*./还有取余:% 整数进行算术操作得到的还是整数,例如整数使用 / 得到的是整数(商的整数部分 ...

  10. JavaScript 深度遍历对象的两种方式,递归与非递归

    递归遍历: 基本问题: 当前属性值不为对象时,打印键和值 递归过程:当前属性值为对象时,打印键,继续递归 var o = { a: { b: { c: { d: { e: { f: 1, g:{ h: ...