Python基础入门视频,无偿分享,鼎力推荐

Python基础入门视频,无偿分享,哈佛大学鼎力推荐

Python基础入门视频,无偿分享,哈佛大学鼎力推荐

哈佛大学推荐,Python基础入门,Python小白书籍,Python学习路线,Python进阶,Python高级,Python爬虫等等一系列关于Python的文档和视频(包括hadoop,fink,hive,MySQL,spark,算法,Linux)

全都打包好了

 

点击此处添加图片说明文字

 

 

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

 

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

点击此处添加图片说明文字

 

python中的字符串

python中的字符串类型为str,也是平时操作的类型。但除了str类型,还有一个专门的名为"string"的模块(导入即可知),是很早以前没有str类型的时候用的,现在几乎不用。

在python 3.x中,字符串的类型str是Unicode的。除此之外还有byte类型、bytearray类型。关于byte和bytearray,参考bytes和bytearray、编码和解码。

一些和字符相关的官方手册:

  1. string模块
  2. str类型详细说明
  3. 字面常量
  4. byte和bytearray
  5. byte和bytearray操作

字符串常量

下面是几个字符串:

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">'malong' 'malong\'s girl friend' # 包含了单引号 'malong\n' # \n换行了 "malong\n" # 换行了 'malong girl friend' # 换行了 'malong\\n' # 没换行 '''malong's girl''' # 包含了单引号 """malong shuai""" # 换行了</code>

python中可以使用单引号、双引号、三引号包围字符串,并可以使用反斜线转义特殊字符:

  • 单、双引号是完全一致的,不像其他语言一样有强、弱引用之分
  • 三引号('''xxx'''"""xxx""")包围的字符完全是字面符号,包围什么就得到什么,包括换行,且不会进行任何转义、替换等使用这种块字符串的方式,在输入需要换行的字符串时非常方便而且可以作为块注释符注释一段代码。这表示这段被包围的代码解释为字符串,但因为没有赋值给变量,所以直接丢弃了
  • 反斜线\可以转义特殊字符,例如在字符串中保留单引号"a\'b"

但是python在输出字符串的时候,总是倾向于使用单引号,除非字符串中包含了单引号。对于那些非打印字符,将总是使用十六进制的方式输出。

例如:

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt "aaa",'aaa' ('aaa', 'aaa') gtgtgt print("aa\na") aa a gtgtgt print('aa\na') aa a gtgtgt '''aaaa ... aaa ... ''' 'aaaa\naaa\n' gtgtgt '\001' # 二进制字符,十六进制格式输出 '\x01'</code>

raw字符串

虽然可以通过反斜线\转义去调整字符串,但带上反斜线有时候会严重影响可读性。如果不想使用反斜线转义,可以考虑使用三引号包围,也可以使用r来声明后面的字符串是raw字符串,这里面的所有字符都是字面意义的,不会进行任何转义。

例如:

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt 'abc\nd' 'abc\nd' gtgtgt len('abc\nd') 5 gtgtgt r'abc\nd' 'abc\\nd' gtgtgt len(r'abc\nd') 6</code>

经常在open()函数打开Windows文件的时候会使用raw字符串。例如下面三种方式:

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">open('d:\new\test.txt') # (1) open('d:\\new\\test.txt') # (2) open(r'd:\new\test.txt') # (3)</code>

(1)中解释字符串的时候,发现里面有\n\t,它们会分别解释为换行符和制表符,这显然不可能是想要打开的文件。所以,在(2)中,使用反斜线将反斜线自身进行了转义。而(3)中使用r声明这是一个raw字符串,里面的转义序列不会进行转义,而是作为普通字符,所以这里的\表示的是路径的分隔符号。

当然,考虑到可移植性的问题,open()函数中直接使用斜线也可以表示windows中的路径分隔符。所以下面是有效的:

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">open('d:/new/test.txt')</code>

注意,raw字符串不能以反斜线结尾,除非对其转义。例如r'abc\ndef\'是错误语法,但r'abc\ndef\\'是正确的语法,它表示后面有两个反斜线字符。

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt r'abc\nd\a\\' 'abc\\nd\\a\\\\' gtgtgt print(r'abc\nd\a\\') abc\nd\a\\ gtgtgt r'abc\nd\a\' File "ltstdingt", line 1 r'abc\nd\a\' ^ SyntaxError: EOL while scanning string literal</code>

字符串转换

数值和字符串类型不能直接放在一起操作,例如9 + "9"是错误的。要将一个数值类型转换成字符串类型,可以使用str()方法或repr()方法。

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt str(8) '8' gtgtgt repr(8) '8'</code>

它们都表示返回字符串类型的数据。

当使用print()输出str()或repr()转换的内容,它们的结果会不一样:

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt print(str('a')) a gtgtgt print(repr('a')) 'a'</code>

一般来说使用str()即可,在学习到__str__()__repr__()之前,没必要去区分str()和repr()。

操作字符串

在python中,操作字符串的方式有多种,大概概括有:

  1. 字符串是一个序列,且是不可变序列,所以序列的通用操作和不可变序列的操作都能应用在str上
  2. string对象自身实现了很多方法,比如大小写转换、子串搜索、截断等等
  3. 字符串连接,如"abc" "def""abc" + "def"是等价的,都是"abcdef""a" * 3得到"aaa"
  4. 字符串格式化(本文不介绍,因为内容太长,见后面的文章)

例如:

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt len("abc") 3 gtgtgt 'abc' 'def' 'abcdef' gtgtgt 'abc' + 'def' 'abcdef' gtgtgt 'abc' * 3 'abcabcabc' gtgtgt 3 * 'abc' 'abcabcabc'</code>

通过字符串的乘法"*"操作,可以轻松地重复给定数量的字符。例如:

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt print("-" * 20) -------------------- gtgtgt print("-" * 20) --------------------</code>

轻松地就能得到长度一样的分割线。

注意,这两个符号不能操作数值、字符串的混合表达式,因为python不会自动转换隐式转换数据类型。

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt "3" + 3 Traceback (most recent call last): File "ltstdingt", line 1, in ltmodulegt TypeError: must be str, not int</code>

因为python中的字符串是一种序列类型,所以可以使用in来测试字符串中是否包含某个字符或子串。

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt 'o' in "hello" True gtgtgt 'a' in "hello" False gtgtgt 'll' in "hello" True</code>

同样,因为是序列类型,可以使用for来遍历整个字符串:

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt str = "longshuai" gtgtgt for i in str: ... print(i, end=",") ... l,o,n,g,s,h,u,a,i,</code>

在for遍历字符串的过程中,控制变量i会取得字符串中的每个字符。

字符串的索引和分片操作

字符串是一种序列,可以应用序列的一种非常方便的分片操作。关于序列的相关操作(包括分片),详细内容见python序列操作,此处介绍一些基本的操作。

必须注意,因为字符串是不可变对象,无法原处修改,所以无论是索引取值还是分片操作,都会创建新字符串对象。

索引取单个元素

例如,通过索引从字符串中取元素:

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt str = "malong" gtgtgt str[0] 'm' gtgtgt str[1] 'a'</code>

索引位可以是负数,表示从尾部开始取元素,-1表示倒数第一个元素,-2表示倒数第二个元素。

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt str[-1] 'g' gtgtgt str[-2] 'n'</code>

所以,-i的索引位等价于len() - i的索引位。

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt str[-1] 'g' gtgtgt str[len(str) - 1] 'g'</code>

分片取多个元素

分片的方式是使用[i:j][i:j:k]的方式,表示从索引位i开始取到索引位j(不包含j),i或j都可以省略。如果指定了k,则表示每隔k个元素取一次,也就是取一次元素之后跳过(k-1)一个元素。i、j、k都可以使用负数。

例如:

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt str = 'malong' gtgtgt str[1:3] 'al'</code>

[1:3]表示从索引位1取到索引位3,不包括3的元素。

可以使用负数的索引位。下面的表示从第3个元素取到倒数第2个元素,但不包含倒数第二个。

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt str[2:-2] 'lo'</code>

i和j都可以使用负数。

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt str[-4:-2] 'lo'</code>

可以省略i或j,或都省略。[:j]表示从头取到索引位为j的元素(不包含j),[i:]表示从索引位i开始取到结尾,[:]表示从头取到尾,也就是拷贝一份字符串。

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt str[:3] 'mal' gtgtgt str[:-1] 'malon' gtgtgt str[1:] 'along' gtgtgt str[-4:] 'long' gtgtgt str[:] 'malong'</code>

指定k时,可以跳过指定间隔的元素。默认为1,表示每隔一个元素取一次,也就是不跳过任何元素。指定为2时表示取1次跳过一个元素。

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt str 'malong' gtgtgt str[::1] 'malong' gtgtgt str[::2] 'mln'</code>

所以,取奇数位的字符和偶数位的字符就很简单了。

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt str[::2] # 取奇数位 'mln' gtgtgt str[1::2] # 取偶数位 'aog'</code>

如果k为负数,则表示反序取元素。例如:

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt 'abcde'[::-1] 'edcba' gtgtgt 'abcde'[::-2] 'eca'</code>

再看下面反序输出的例子。

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt 'abcdefgh'[5:0:-1] 'fedcb'</code>

表示反序0-5的元素abcdef,但不包含0位,即得到fedcb。也就是说得到索引位1-5的反序结果。

所以:

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt 'abcdefgh'[5:0:-2] 'fdb'</code>

字符串方法

因为内容较多,本人已从官方手册翻译并整理成单独的文章,见python字符串方法整理

修改字符串

字符串是不可变数据类型,要修改字符串,只能通过分片新建字符串对象并进行合并,或者直接使用字符串方法。

例如:

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt s = "hello" gtgtgt s[0] = "H" Traceback (most recent call last): File "ltstdingt", line 1, in ltmodulegt TypeError: 'str' object does not support item assignment</code>

要修改第一个元素,可以从原始字符串中切片取出后面的字符,并结合修改的目标字符构建一个新的字符串,并赋值给原始变量。

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt s = "hello" gtgtgt s = "H" + s[1:] gtgtgt s 'Hello'</code>

再例如,将一个新字符串追加到源字符串的尾部:

<code class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, quotAndale Monoquot, quotlucida consolequot, quotCourier Newquot, monospace; font-size: inherit; background-color: inherit;">gtgtgt s = "hello" gtgtgt s = s + " world" gtgtgt s 'hello world' gtgtgt s = "hello world" gtgtgt s = s[:6] + "your " + s[6:] gtgtgt s 'hello your world' gtgtgt s = "hello world" gtgtgt where = s.find("world") gtgtgt where 6 gtgtgt s = s[:where] + "your " + s[where:] gtgtgt s 'hello your world'</code>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值