一、python基础之字符串的处理
2024-10-07 14:42:46
最近开始重新回过头来巩固一下python的基础知识,并在此做一些记录以便未来更好的回顾
一、字符串的大小写转换
title()
使用title()方法可以将字符串中每个单词的首字母大写
name = "hello world"
print(name.title())
运行得到结果:
Hello World
upper()
使用upper()方法可以将字符串中的每一个字母转换为大写
name = "hello world"
print(name.upper())
运行得到结果:
HELLO WORLD
lower()
使用lower()方法可以将字符串中的所有字母转换为小写
name = "HELLO WORLD"
print(name.lower())
运行得到结果:
hello world
二、拼接字符串
使用+对字符串进行拼接
first = "hello"
last = "world"
add = first + " " + last
print(add)
运行后得到结果:
hello world
三、使用制表符和换行符
制表符
制表符可以使用字符组合\t
print("\tpython")
运行得到结果:
#输出结果前有一个空格
python
换行符
print("languages:\nPython\nJava")
运行得到结果:
languages:
Python
Java
四、删除空白
删除末尾的空白
使用rstrip()
name = "python "
print(name.rstrip())
运行得到结果:
python
删除开头的空格
使用lstrip()
name = " python"
print(name.lstrip())
运行得到结果:
python
同时剔除开头和结尾的空格
使用strip()
name = " python "
print(name.strip())
运行得到结果:
python
最新文章
- win 7下建立FTP
- poj2236_并查集_Wireless Network
- nginx 启动、重启、关闭
- [GodLove]Wine93 Tarining Round #8
- 在linux上使用交换文件扩展交换空间
- jQuery 2.0.3 源码分析 Deferrred概念
- 创建本地Ubuntu镜像
- 合并大量txt文件的内容
- 使用python网络库下载
- Http方式获取网络数据
- JSP/Servlet(一)
- 【有意思的BUG】分享按钮 分享功能
- #include <;iostream>;与#include <;iostream.h>;的区别
- CSS透明opacity和IE各版本透明度滤镜filter的准确用法
- Asp.Net SignalR Hub类中的操作详解
- WPF样式中TargetType 属性 (Property) 和 x:Key 属性 (Attribute)
- npm install --save 、--save-dev 、-D、-S 的区别
- LINUX内核PCI扫描过程
- [Swift]JSON字符串与字典(Dictionary)、数组(Array)之间的相互转换
- centos6.8安装httpd后无法访问