Python基础-Python字符串处理的十个小规则

字符串处理在日常Python编程中是用的比较多的操作。这里列一下Python字符串处理的一些实用技巧和使用方式,避免踩坑。

1.双引号与单引号

Python中定义字符串时,单引号和双引号是有区别的:

对于没有特殊字符的,双引号和单引号都一样:

>>> "Hello,world!"``'Hello,world!'``>>> 'Hello,world!'``'Hello,world!'

对于字符串内容带特殊标点和字符的,就需要注意了

>>> "'Nic' is s student"``"'Nic' is s student"``>>> ''Nic' is s student'`  `File "", line 1`    `''Nic' is s student'`        `^``SyntaxError: invalid syntax``>>> '"Nic" is s student'``'"Nic" is s student'

2.转义符

双引号就不需要转义

>>> "Let's go!"``"Let's go!"

单引号,则需要转义:

>>> 'Let's go!'`  `File "", line 1`    `'Let's go!'`         `^``SyntaxError: invalid syntax
>>> 'Let\'s go!'``"Let's go!"

双引号转义

>>> "\"Hello World!\" she said"``'"Hello World!" she said'

3.连接字符串

将2个字符串连接起来:

直接连起来写

用双引号:

>>> "Hello" "World"``'HelloWorld'`          `   ``>>> "Let's say" '"Hello World!"'``'Let\'s say"Hello World!"'`          `   ``>>> "Let\'s say" '"Hello World!"'``'Let\'s say"Hello World!"'

注意看上面的,Python打印的字符串还是被引号括起来,并加了转义符

–需要使用print打印

>>> print "Let's say" '"Hello World!"'``Let's say"Hello World!"`          `   ``>>> print "Let\'s say" '"Hello World!"'``Let's say"Hello World!"

4.变量连接

>>> x="Hello"``>>> y="World"

这样连接会报错:

>>> x y`  `File "", line 1`    `x y`      `^``SyntaxError: invalid syntax

这样也不行:

>>> x-y``Traceback (most recent call last):`  `File "", line 1, in``TypeError: unsupported operand type(s) for -: 'str' and 'str'

要使用+号

>>> x+y``'HelloWorld'

中间加空格

>>> x+" "+y``'Hello World'

5.str与repr

将值转换为字符串

str函数–把值转换为合理形式的字符串

repr函数 创建一个字符串,以合法的Python表达式的形式表示值

对比二者的区别

>>> print repr("Hello, World!")``'Hello, World!'`          `   ``>>> print repr(10000L)``10000L`          `   ``>>> print str("Hello, World!")``Hello, World!`          `   ``>>> print str(10000L)``10000

repr也可以用反引号实现

>>> temp=37``>>> print temp``37
>>> print "Temp is " + temp``Traceback (most recent call last):`  `File "", line 1, in``TypeError: cannot concatenate 'str' and 'int' objects

单引号引用还是字符串内容:

>>> print "Temp is " + 'temp'``Temp is temp

反引号才能引用到变量值

` >>> print "Temp is " + `temp` ```Temp is 37

6.键盘输入****raw_input与input

>>> input ("Enter a number: ")``Enter a number: 100``100
>>> raw_input ("Enter a number: ")``Enter a number: 100``'100'
>>> name = input("What's your name? ")``What's your name? Jim``Traceback (most recent call last):`  `File "", line 1, in`  `File "", line 1, in``NameError: name 'Jim' is not defined
>>> name = raw_input("What's your name? ")``What's your name? jim``>>> print name``jim

区别看出来了吗?raw_input函数将所有的输入当做原始数据 将其放入字符串中

**7.**使用三引号跨行

直接换行会报错:

>>> print "Hello #敲回车报错`  `File "", line 1`    `print "Hello`               `^``SyntaxError: EOL while scanning single-quoted string

可以使用三引号:

>>> print '''Hello``... World,``... Hello,``... Jim.``... over!'''``Hello``World,``Hello,``Jim.``over!

一个小例子



def hello(): print 'i am test3' if name == ‘main’: a=10 b=‘’'hello hello2 hello3 hello4''' print b



D:\Python27\python.exe D:/sync/asp_notes_all/python/tuji/lesson01/test/testTT3.py``hello`    `hello2`    `hello3`    `hello4``Process finished with exit code 0

8.使用斜线跨行

>>> print "Hello,\``... World!"``Hello,World!`          `   ``>>> print \``... 'Hello World'``Hello World`          `   ``>>> print \``... "Hello World"``Hello World`          `   ``>>> 1+2+ \``... 3``6

9 \n换行

>>> path = 'C:\note'``>>> path``'C:\note'``>>> print path``C:``ote
>>> path = 'C:\\note'``>>> path``'C:\\note'``>>> print path``C:\note

10 引用原始字符串

但是如果要转义的很多,就有点繁琐–引出原始字符串的用途

`>>> print r'Let\'s go!'``Let\'s go!`          `   ``>>> print r'C\note'``C\note`          `   ``>>> print r'C:\note'``C:\note`          `   ``>>> print r'D:\1-abseedb\sync_docs_dba\asp-db'``D:\1-abseedb\sync_docs_dba\asp-db`          

原始字符串最后一个不能是反斜线

>>> print r'C:\note'``C:\note`          `   ``>>> print r'C:\note\'`  `File "", line 1`    `print r'C:\note\'`                    `^``SyntaxError: EOL while scanning single-quoted string

解决办法:

>>> print r'C:\note' '\\'``C:\note\

11 Unicode字符串

Unicode字符串使用u前缀,就像使用r一样

>>> u'Hello, World``u'Hello, World'

说明:

Python的普通字符串在内部是以8位的ASC码形成存储的

Unicode字符串则存储为16位的Unicode字符

以上小规则,在实际编程中需要注意区分和正确使用,避免字符串处理出错。

在这里插入图片描述

---------------------------END---------------------------

题外话

“不是只有程序员才要学编程?!”

认真查了一下招聘网站,发现它其实早已变成一项全民的基本技能了。

连国企都纷纷要求大家学Python!
在这里插入图片描述

世界飞速发展,互联网、大数据冲击着一切,各行各业对数据分析能力的要求越来越高,这便是工资差距的原因,学习编程顺应了时代的潮流。

在这个大数据时代,从来没有哪一种语言可以像Python一样,在自动化办公、爬虫、数据分析等领域都有众多应用。

更没有哪一种语言,语法如此简洁易读,消除了普通人对于“编程”这一行为的恐惧,从小学生到老奶奶都可以学会。

《2020年职场学习趋势报告》显示,在2020年最受欢迎的技能排行榜,Python排在第一。
在这里插入图片描述

它的角色类似于现在Office,成了进入职场的第一项必备技能。

如果你也想增强自己的竞争力,分一笔时代的红利,我的建议是,少加点班,把时间腾出来,去学一学Python。

因为,被誉为“未来十年的职场红利”的Python,赚钱、省钱、找工作、升职加薪简直无所不能!

目前,Python人才需求增速高达**174%,人才缺口高达50万,**部分领域如人工智能、大数据开发, 年薪30万都招不到人!在这里插入图片描述

感兴趣的小伙伴,赠送全套Python学习资料,包含面试题、简历资料等具体看下方。

一、Python所有方向的学习路线

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照下面的知识点去找对应的学习资源,保证自己学得较为全面。

img
img

二、Python必备开发工具

工具都帮大家整理好了,安装就可直接上手!img

三、最新Python学习笔记

当我学到一定基础,有自己的理解能力的时候,会去阅读一些前辈整理的书籍或者手写的笔记资料,这些笔记详细记载了他们对一些技术点的理解,这些理解是比较独到,可以学到不一样的思路。

img

四、Python视频合集

观看全面零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。

img

五、实战案例

纸上得来终觉浅,要学会跟着视频一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。img

六、面试宝典

在这里插入图片描述

在这里插入图片描述

简历模板在这里插入图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值