惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
A
Arctic Wolf
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Troy Hunt's Blog
Latest news
Latest news
Help Net Security
Help Net Security
S
Security Affairs
Webroot Blog
Webroot Blog
The Hacker News
The Hacker News
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tor Project blog
Forbes - Security
Forbes - Security
Google DeepMind News
Google DeepMind News
AWS News Blog
AWS News Blog
Attack and Defense Labs
Attack and Defense Labs
P
Proofpoint News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Help Net Security
L
Lohrmann on Cybersecurity
S
SegmentFault 最新的问题
Google Online Security Blog
Google Online Security Blog
MongoDB | Blog
MongoDB | Blog
Cyberwarzone
Cyberwarzone
The Last Watchdog
The Last Watchdog
S
Securelist
N
News and Events Feed by Topic
S
Secure Thoughts
F
Fortinet All Blogs
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
量子位
M
MIT News - Artificial intelligence
F
Full Disclosure
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
P
Privacy International News Feed
L
LangChain Blog
Know Your Adversary
Know Your Adversary
C
CERT Recently Published Vulnerability Notes

博客园 - lovingbird

Python中的数字 vs2015中SQLSERVER数据库项目引用系统视图 SQL Server 2008通过LinkServer连接MySQL 删除sqlserver代理任务脚本 ldap实现用户认证 mysql下将分隔字符串转换为数组 从ICassFactory为CLSID为{17BCA6E8-A950-497E-B2F9-AF6AA475916F}的COM组件创建实例失败问题解决方法 IIS7下ajax报未定义错误 Access restriction错误解决办法 通过.browser文件设置chrome等浏览器的兼容性 通过web.config设置网站默认页 NC调试时客户端报错:nc.ui.sm.login.Loader2.<init> 本地连接与无线网络连接消失的解决办法 asp.net中使用ajax提示“'Sys'未定义”错误 解决ora-12514 问题 SQLServer常用命令 WIN7的各种安装方法 将WebBrowser的cookie信息传给HttpWebRequest - lovingbird - 博客园 Access建表语句 - lovingbird - 博客园
Python中的字符串常用处理方法
lovingbird · 2022-12-04 · via 博客园 - lovingbird

字符串

字符串就是一系列的字符。在Python中,用引号括起的都是字符串,其中的引号可以是单引号,也可以是双引号,如下所示:

"This is a string."
'This is also a string.'

这种灵活性让你能够在字符串中包含引号和撇号:

'I told my friend, "Python is my favorite language!"'
"The language 'Python' is named after Monty Python, not the snake."
"One of Python's strengths is its diverse and supportive community."

字符串的处理

1.使用方法修改字符串的大小写

name = "ada lovelace"
print(name.title())
name = "Ada Lovelace"
print(name.upper))
print(name.lower))

上方代码的执行结果

Ada Lovelace
ADA LOVELACE
ada lovelace

存储数据时,方法lower()很有用。很多时候,你无法依靠用户来提供正确的大小写,因此需要将字符串先转换为小写,再存储它们。以后需要显示这些信息的时候,再将其转换为合适的大小写。

2.合并(拼接)字符串

Python使用加号(+)来合并字符串,例如:

first_name = "ada"
last_name = "lovelace"
full_name = first_name +" " + last_name
print("Hello, " + full_name.title() + "!")

例中,一个问候用户的句子中使用了全名,并使用了方法title()来将姓名设置为合适的格式,最终显示一条格式良好的简单问候语:

3.使用制表符或换行符来添加空白

在编程中,空白泛指任何非打印字符,如空格、制表符和换行符。要在字符串中添加制表符,可使用字母组合\t,如下述代码所示

>>> print("Python")
Python
>>> print("\tPython") 
    Python

要在字符串中添加换行符,可使用字符组合\n,如下述代码所示

>>> print("Languages:|nPython\nCInJavaScript")

输出结果如下

Languages:
Python
C
JavaScript

当然,这些字符也可以组合到同一字符串中进行输出,效果如何,可自行测试。

4.删除空白

在程序中,额外的空白可能令人迷惑。对程序员来说,'python'和python‘看起来几乎没什么两样,但对程序来说,它们却是两个不同的字符串。Pyhon 能够发现python‘中额外的空白,并认为它是有意义的——除非你告诉它不是这样的。
空白很重要,因为你经常需要比较两个字符串是否相同。例如,一个重要的示例是,在用户登录网站时检查其用户名。但在一些简单得多的情形下,额外的空白也可能令人迷惑。所幸在Python 中,删除用户输入的数据中的多余的空白易如反掌。
Python 能够找出字符串开头和末尾多余的空白。要确保字符串末尾没有空白,可使用方法rstrip()。

>>> favorite_language = 'python ' 
>>> favorite_language 
'python '
>>> favorite_language.rstrip() 
'python'
>>> favorite_language 
'python '

类似的方法还有lstrip()和strip(),分别用作剔除字符串左侧的空白字符串、剔除字符串两端的空白。在实际程序中,这些剔除函数最常用于在存储用户输入前对其进行清理。

5.使用字符串时避免语法错误

语法错误是一种时不时会遇到的错误。程序中包含非法的Python代码时,就会导致语法错误。例如,在用单引号括起的字符串中,如果包含撇号,就将导致错误。这是因为这会导致Python将第一个单引号和撇号之间的内容视为一个字符串,进而将余下的文本视为Python代码,从而引发错误。

注意 编写程序时,编辑器的语法突出功能可帮助你快速找出某些语法错误。看到Python代码以普通句子的颜色显示,或者普通句子以Python代码的颜色显示时,就可能意味着文件中存在引号不匹配的情况。

6.Python2中的print语句

在Python 2中,print语句的语法稍有不同: > python2.7
>>> print "Hello Python 2.7 world!"
Hello Python 2.7 world!
在Python 2中,无需将要打印的内容放在括号内。从技术上说,Python 3中的print是一个函数,因此括号必不可少。有些Python 2 print语句也包含括号,但其行为与Python 3中稍有不同。简单地说,在Python 2代码中,有些print语句包含括号,有些不包含。