1、string.issupper()表示判断字符是否全部为小写字母。

 string1 = "abcdef"
string2 = "ABCdef"
string3 = "ABCDEF"
print(string1.isupper())
print(string2.isupper())
print(string3.isupper())
结果为:
False
False
True

2、string.join(seq)用“string”字符将seq连接为一个字符串,seq可以为list,tuple,dirct,string类型,例如

 January+Febrary+Match+April+May+Jane+July+Augest
January#Febrary#Match#April#May#Jane#July#Augest
January*Febrary*Match*April*May*Jane*July*Augest
结果为:
January+Febrary+Match+April+May+Jane+July+Augest
January#Febrary#Match#April#May#Jane#July#Augest
January*Febrary*Match*April*May*Jane*July*Augest

3、string.ljust(参数1,参数2)表示左对齐,参数1表示总得字符宽,当string的宽度小于参数1给定的长度时,则右侧以参数2填充

string.rjust()表示右对齐,当参数string的宽度小于参数1时,左侧用参数2填充。当string的长度大于参数1时,什么也不做。

 a = "Hello World"
print(a.ljust(15,'-'))
print(a.rjust(15,'+'))
print(a.ljust(10,'='))
print(a.ljust(10,'&'))
结果为:
Hello World----
++++Hello World
Hello World
Hello World

4、string.lower()表示将string变为小写字符。

 a = "abcdefg"
print(a.lower())
b = "ABCDEFG"
print(a.lower())
c = "ABCdefg"
print(c.lower())
结果为:
abcdefg
abcdefg
abcdefg

5、string.lstrip()表示修剪string左侧的回车和空格字符

string.rstrip()表示修剪string右侧的回车和空格字符

string.strip()表示修剪string左右两侧的回车和空格字符

 string_1 = "    abcdefg     "
print(string_1.lstrip())
print(string_1.rstrip())
print(string_1.strip())
结果为:
abcdefg
abcdefg
abcdefg

6、trantab = str.maketrans(intab,outab)表示将intab转化为相应的outab,string.translate(trantab)表示按trantab的映射方法翻译string的内容,如果intab和outab没有相对应的映射,则保留string中原有的形式。

 a = 'abcdefg'
b = ''
string = "This is a beautiful world , Welcome!!"
trantab = str.maketrans(a,b)
trans = string.translate(trantab)
print(trans)
结果为:
This is 1 251uti6ul worl4 , W5l3om5!!

7、string.partition(参数1)表示将字符串string按参数1的分隔符分隔,生成一个元组,且此元组的元素个数为2

 string = "www.baidu.com"
str_tuple = string.partition('.')
print(str_tuple)
结果为:
('www', '.', 'baidu.com')

注:由于元组的元素个数为3,所以“baidu.com”的这个“ . ”没有分开

8、string.repalce(old,new,max)用新的字符替换旧的字符,最多替换次数max

 string = "this is a wonderful world, this is the first time to meet Python"
print(string.replace('is','was',3))
结果为:
thwas was a wonderful world, thwas is the first time to meet Python

9、string.split(参数1,参数2)将string按参数1的分隔符分隔参数2的次数

 string = "this is why Python is wonderful!"
print(string.split(' '))
print(string.split(' ',3))
结果为:
['this', 'is', 'why', 'Python', 'is', 'wonderful!']
['this', 'is', 'why', 'Python is wonderful!']

注:此和序号为7的partition()不同,split生成的是一个list,partition生成的是一个元组且元素数为3,split则没有限制

10、string.splitlines(参数)表示按换行符和回车符为分隔符,生成一个list,当没有参数是只是按"\t","\r"分隔,当参数为True时,则按"\t","\r"分隔,且保留"\t","\r"

 string = "ab c\n\nde fg\rkl\r\n"
str_1 = string.splitlines()
str_2 = string.splitlines(True)
print(str_1)
print(str_2)
结果为:
['ab c', '', 'de fg', 'kl']
['ab c\n', '\n', 'de fg\r', 'kl\r\n']

11、string.startwith(str,参数1,参数2)判断字符串string是否是以str开始的,参数1表示开始判断的位置,参数2表示结束的位置

 string = "this is a wonderful world!"
str_1 = string.startswith('this')
str_2 = string.startswith('th')
str_3 = string.startswith('is',2,5)
str_4 = string.startswith('is',3,5) #虽然str_1、str_2、str_3均为True,但是str_4为False,说明是按空格分隔后才比的,否则str_4应为True。 print(str_1)
print(str_2)
print(str_3)
print(str_4) 结果为:
True
True
False
False
False

12、string.swapcase()将大小写字母相互转换

 a = "This Is a Wonderful World!"
print(a.swapcase())
结果为:
tHIS iS A wONDERFUL wORLD!

13、string.title()将字符string转化为标题类型

 a = "this is a wonderful world!"
print(a.title())
结果为:
This Is A Wonderful World!

14、upper()转化为大写

 a = "this is a wonderful world!"
print(a.upper())
结果为:
THIS IS A WONDERFUL WORLD!

15、string.zfill()返回指定长度的字符串,原字符串右对齐,不足则在前边不“0”

 a = "this is a wonderful world!"
print(a.zfill(5))
print(a.zfill(40))
print(a.zfill(50))
结果为:
this is a wonderful world!
00000000000000this is a wonderful world!
000000000000000000000000this is a wonderful world!

Python之字符(2)的更多相关文章

  1. Python基础-字符编码与转码

    ***了解计算机的底层原理*** Python全栈开发之Python基础-字符编码与转码 需知: 1.在python2默认编码是ASCII, python3里默认是utf-8 2.unicode 分为 ...

  2. Python中文字符的理解:str()、repr()、print

    Python中文字符的理解:str().repr().print 字数1384 阅读4 评论0 喜欢0 都说Python人不把文字编码这块从头到尾.从古至今全研究通透的话是完全玩不转的.我终于深刻的理 ...

  3. Python的字符编码

    Python的字符编码 1. Python字符编码简介 1. 1  ASCII Python解释器在加载.py文件的代码时,会对内容进行编码,一般默认为ASCII码.ASCII(American St ...

  4. Python常用字符编码(转)

    Python常用字符编码   字符编码的常用种类介绍 第一种:ASCII码 ASCII(American Standard Code for Information Interchange,美国信息交 ...

  5. python生成字符画

    python生成字符画 这个idea来自于实验楼,非常适合练习PIL的像素处理,更重要的是非常有意思. 环境配置 依赖的第三方库就是PIL(Python Image Library),可以直接使用pi ...

  6. Python常见字符编码间的转换

    主要内容:     1.Unicode 和 UTF-8的爱恨纠葛     2.字符在硬盘上的存储     3.编码的转换     4.验证编码是否转换正确     5.Python bytes类型 前 ...

  7. python 3字符编码

    python 3字符编码 官方链接:http://legacy.python.org/dev/peps/pep-0263/ 在Python2中默认是ascii编码,Python3是utf-8编码 在p ...

  8. python文本 字符与字符值转换

    python文本 字符与字符值转换 场景: 将字符转换成ascii或者unicode编码 在转换过程中,注意使用ord和chr方法 >>> print(ord('a'))    97 ...

  9. (转)Python格式化字符 %s %d %f

    Python格式化字符 %s %d %f 原文:http://blog.csdn.net/huangfu77/article/details/54807835 格式 描述%% 百分号标记 #就是输出一 ...

  10. 【已解决】python中文字符乱码(GB2312,GBK,GB18030相关的问题)

      http://againinput4.blog.163.com/blog/static/1727994912011111011432810/ [已解决]python中文字符乱码(GB2312,GB ...

随机推荐

  1. 【资源分享】半条命2速通AHK脚本

    *----------------------------------------------[下载区]----------------------------------------------* ...

  2. 2019冬季PAT甲级第二题

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; typedef struct{ int ...

  3. linux日常运维工作

    Linux的使用环境也日趋成熟,各种开源产品络绎不绝,大有百花齐放的盛景,那么当Linux落地企业,回归工作时,我们还要面对这Linux运维方面的诸多问题,今天我们特意组织一场有关Linux 在企业运 ...

  4. C++记录(一)

    1 extern 符表示该变量不是当前作用域定义的,用于声明. 如extern i;表示i不是当前作用域里的,是其他某个include的cpp文件里的变量. 2 int *p=0;相当于初始化p为空指 ...

  5. Linux控制服务和守护进程

    目录 控制服务和守护进程 1.systemd 1.1.systemd简介 1.2.systemd的新特性 1.3.systemd的核心概念Unit 2.使用systemctl管理服务 控制服务和守护进 ...

  6. Laravel Vuejs 实战:开发知乎 (9)定义话题与问题关系

    1.话题[Topic] 执行命令: php artisan make:model Topic –cmr 修改****_**_**_create_topics_table.php数据库迁移文件如下: c ...

  7. FYF的煎饼果子

    利用等差数列公式就行了,可以考虑特判一下m >= n($ m, n \neq 1 $),这时一定输出“AIYAMAYA”. #include <iostream> using nam ...

  8. 【Go语言系列】1.1、GO语言简介:什么是GO语言

    一.Go的起源 Go语言的所有设计者都说,设计Go语言是因为 C++ 给他们带来了挫败感.在 Google I/O 2012 的 Go 设计小组见面会上,Rob Pike 是这样说的: 我们做了大量的 ...

  9. 兵贵神速!掌握这10个Python技巧,让你代码工作如鱼得水

    主题 Python 1000个读者心中有1000个哈姆雷特,要问1000个程序员“什么才是最好的语言”,Java.Python.PHP.C++ 也都有自己的位置.但要问编程语言流行指数之王非,那真的非 ...

  10. JavaEE实战——XML文档DOM、SAX、STAX解析方式详解

    原 JavaEE实战--XML文档DOM.SAX.STAX解析方式详解 2016年06月22日 23:10:35 李春春_ 阅读数:3445 标签: DOMSAXSTAXJAXPXML Pull 更多 ...