python3 字符串相关函数
python版本 3.5
#Author by Liguangbo
#_*_ coding:utf-8 _*_
str="i like study python, welcome to my python program\t."
#首字母大写
print(str.capitalize())
#I like study python, welcome to my python program.
#关键字在字符串中出现的次数
print(str.count(" "))
#8
#打印100个字符,如果str不够,则用-代替,且字符str位于中间
print('hello world'.center(20,'-'))
#----hello world-----
#判断字符串是否以‘l’和‘.’开头结尾
print(str.startswith('l'))
#False
print(str.endswith('.'))
#True
#将tab键转为5个空格
print(str.expandtabs(tabsize=51))
#i like study python, welcome to my python program .
#查找第一个sub出现的位置
sub='p'
print(str[str.find(sub):])
#python, welcome to my python program .
#字符串的参数调用及赋值
s="my name is {name},i am {years} years old!"
print(s.format(name="ligb",years="28"))
print(s.format_map({'name': 'ligb' ,'years':28}))
#my name is ligb,i am 28 years old!
#判断是否是由阿拉伯数字或字母组成,不能包含符号、空格
x='我'
print(x.isalnum())
#True
#判断是否是纯字符,不能包含数字或者符号
print(x.isalpha())
#True
print('一'.isdecimal())
#False
print('1'.isdigit())
#True
#判断是否是小写、大写
print('a'.islower())
#True
print('a'.isupper())
#False
#判断是否所有单词首字母大写
print('My Name Is '.istitle())
#True
#判断文件是否可以打印
print('my name is ligb'.isprintable())#tty drive等文件不可打印
#True
#列表转字符串
print('%'.join(['wo','men','de','jia']))
#wo%men%de%jia
#若字符串长度不够20,则在末尾加*补充
print('hello world'.ljust(20,'*'))
#hello world*********
print('hello world'.rjust(20,'*'))
#*********hello world
#大小写转换
print('hello world'.lower())
print('hello world'.upper())
#hello world
#HELLO WORLD
#去掉首尾的回车或者换行
print(' hello world\n'.strip())
print('-----')
#hello world
#-----
#去掉左右的回车或者换行
print(' hello world\n'.rstrip())
print(' hello world\n'.lstrip()) #查找最右边的关键字
print('hello world !'.rfind('world'))
#以空格为分割符,生成列表
print(' '.join('hello world my name is'.split()))
print('hello world my name is'.split())
#['hello', 'world', 'my', 'name', 'is']
print('hello+world+my+name+is'.split('+'))
#['hello', 'world', 'my', 'name', 'is']
#按照换行来分
print('hello \n world'.splitlines())
#['hello ', ' world']
#调换大小写
print('Hello World'.swapcase())
#hELLO wORLD
print('hello world'.title())
#Hello World
python3 字符串相关函数的更多相关文章
- 009.Python字符串相关函数
字符串相关函数 1 capitalize 字符串首字母大写 strvar = "this is a dog" res = strvar.capitalize() print(res ...
- python3字符串
Python3 字符串 Python字符串运算符 + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符串 a*2 输出结果:HelloHello [] 通过索引获取字符串中 ...
- Perl函数:字符串相关函数
Perl字符串相关函数 字符串的内置函数有: chomp, chop, chr, crypt, fc, hex, index, lc, lcfirst, length, oct, ord, pack, ...
- [转]python3字符串与文本处理
转自:python3字符串与文本处理 阅读目录 1.针对任意多的分隔符拆分字符串 2.在字符串的开头或结尾处做文本匹配 3.利用shell通配符做字符串匹配 4.文本模式的匹配和查找 5.查找和替换文 ...
- python3字符串操作
python3字符串操作 x = 'abc' y = 'defgh' print(x + y) #x+y print(x * ) #x*n print(x[]) #x[i] print(y[:-]) ...
- db2字符串相关函数的使用
db2字符串相关函数的使用 from :internet 一.字符转换函数 1.ASCII() 返回字符表达式最左端字符的ASCII 码值.在ASCII()函数中,纯数字的字符串可不用‘’括起来 ...
- 【2】python3字符串的比较(辨析is与==的区别)
PYTHON3基本数据类型(二.字符串) Python3字符串 ①字符串比较 1.比较字符串是否相同: ==:使用==来比较两个字符串内的value值是否相同 is:比较两个字符串的id值. 2.字符 ...
- python系列四:Python3字符串
#!/usr/bin/python #Python3 字符串#可以截取字符串的一部分并与其他字段拼接var1 = 'Hello World!'print ("已更新字符串 : ", ...
- python3 字符串属性(一)
python3 字符串属性 >>> a='hello world' >>> dir(a) ['__add__', '__class__', '__contains_ ...
随机推荐
- git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree fetch origin
git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree fetch origi ...
- spring MVC配置详解
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- wkwebview a target="_blank" 打不开链接的解决方案
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigatio ...
- http.Handler 与Go的错误处理
原文地址 在之前我写过一篇关于通过使用http.HandlerFunc来实现一个定制handler类型用来避免一些平常的错误的文章.func MyHandler(w http.ResponseW ...
- java入门之从C#快速入门java
可变参数: Void sum(int…list){}以“…”三个字为关键字 可变参数:以params为关键字 34 对异常的描述: java中,异常分为两种,运行时异常(也就是uncheckExcep ...
- 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数004·edge,边缘处理
<zw版·Halcon-delphi系列原创教程> Halcon分类函数004·edge,边缘处理 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替换:“ ...
- mysql出现“SELECT list is not in GROUP BY clause and contains nonaggregated column [duplicate]”错误提示
项目跨平台时由于mysql设置的问题,原代码运行出现这个错误,此时把mysql设置改下就好了 sql_mode='NO_ENGINE_SUBSTITUTION'
- Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
有3个对象,对象A,对象B,对象C.他们的实体关系为: 1.A中存在List<B>和List<C>,即一个包含另外两个: 2.A中存在List<B>,B中存在Lis ...
- [转] GitHub上README.md教程
点击阅读原文 最近对它的README.md文件颇为感兴趣.便写下这贴,帮助更多的还不会编写README文件的同学们. README文件后缀名为md.md是markdown的缩写,markdown是一种 ...
- mysql重复索引、冗余索引、未使用索引的定义和查找
1.冗余和重复索引 mysql允许在相同列上创建多个索引,无论是有意还是无意,mysql需要单独维护重复的索引,并且优化器在优化查询的时候也需要逐个地进行考虑,这会影响性能.重复索引是指的在相同的列上 ...