首页 > 编程语言 > 详细

一、python基础之字符串的处理

时间:2019-10-07 20:25:11      阅读:66      评论:0      收藏:0      [点我收藏+]

最近开始重新回过头来巩固一下python的基础知识,并在此做一些记录以便未来更好的回顾

一、字符串的大小写转换

title()

使用title()方法可以将字符串中每个单词的首字母大写

1 name = "hello world"
2 print(name.title())

运行得到结果:

Hello World

 

upper()

使用upper()方法可以将字符串中的每一个字母转换为大写

1 name = "hello world"
2 print(name.upper())

运行得到结果:

HELLO WORLD

 

lower()

使用lower()方法可以将字符串中的所有字母转换为小写

1 name = "HELLO WORLD"
2 print(name.lower())

运行得到结果:

hello world

 

二、拼接字符串

使用+对字符串进行拼接

1 first = "hello"
2 last = "world"
3 add = first + " " + last
4 print(add)

运行后得到结果:

hello world

 

三、使用制表符和换行符

制表符

制表符可以使用字符组合\t

1 print("\tpython")

运行得到结果:

#输出结果前有一个空格
    python

 

换行符

1 print("languages:\nPython\nJava")

运行得到结果:

languages:
Python
Java

 

四、删除空白

删除末尾的空白

使用rstrip()

1 name = "python "
2 print(name.rstrip())

运行得到结果:

python

 

删除开头的空格

使用lstrip()

1 name = " python"
2 print(name.lstrip())

运行得到结果:

python

 

同时剔除开头和结尾的空格

使用strip()

1 name = " python "
2 print(name.strip())

运行得到结果:

python

 

一、python基础之字符串的处理

原文:https://www.cnblogs.com/lw-whatever/p/11631884.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!