python初学者程序员

跟我读python3文档:010_字符串(3.1.2. Stri

2017-08-27  本文已影响124人  咖喱py

贯穿始终的理念:别废话,就是干!


往期回顾

import sys
sys.argv[ 0 ]

文档解读

python3文档第三小节链接地址:3. An Informal Introduction to Python

本章主要讲解3.1.2. Strings(字符串)这个小节


3.1.2. Strings(字符串)

段落截取(一):

Besides numbers, Python can also manipulate strings, which can be expressed in several ways. They can be enclosed in single quotes ('...') or double quotes ("...") with the same result .
\ can be used to escape quotes:

名词解释

  1. strings
    中文翻译:字符串
    用引号括起来的数据形式,就是字符串类型。
    注意,引号一定是在英文输入法下输入的。
    前几期讲过,字符串其实就是画。本身没有实际意义,主要是好看。

  2. quotes
    中文翻译:引号
    用引号括起来的就是字符串。无论是单引号还是双引号。

  3. escape
    中文翻译:转义
    反斜杠 \ 代表转义字符。他的作用是什么呢?
    我们来看一个例子:

print('我是咖喱py')

注:在python解释器中,省略print函数,直接输入字符串也是可以得到相同结果的,但是由于直接输入对转义字符很不友好,所以,这里加上print函数。

我们知道,有引号括着的数据形式,就是字符串类型,也就是字符串。
那么,如果这句话换成英文表达会发生什么?

print('I am galipy')

如果缩写一下呢?

print('I'm galipy')

试着在python解释器里自己输入看看会发生什么?

你会发现,解释器报错了。
我们说,用引号括着的数据形式就是字符串,那么,是'I' 还是'm galipy'或者是 'i'm galipy' ?
那么要怎样解决这个问题呢?
有一个简单的办法:

print("I'm galipy")

但是如果是一句更复杂的语句要怎么办呢?
比如:

print(he said:"I'am galipy")

这句话要怎么作为字符串呢?

所以,为了解决这种问题,就要用到转义字符。

print('he said:"I\'am galipy"')

自己输入看看,试着搞明白里面的引号都是什么作用。

转义字符其实就是一些以反斜杠\开头的特殊符号组合。他们在字符串中有特殊的意义。
常见的转义字符如下:

  1. \n
    反斜杠n代表换行。
  2. \'
    反斜杠单引号代表单引号。
  3. \"
    反斜杠双引号代表双引号。

注:如果不会区分斜杠与反斜杠,看看字母A,第一笔就是正斜杠,第二笔就是反斜杠~

段落大意

用单引号或双引号括着的数据形式就是字符串,可以用\转义引号。


段落截取(二):

Strings can be concatenated (glued together) with the + operator, and repeated with *

名词解释

这段只要认识两个单词就好了

  1. concatenated 连接
  2. repeated 重复

段落大意

连接两个字符串可以用加号,重复一个字符串可以用乘号。

>>>'gali' + 'py'
'galipy'
>>> 3 * 'un' + 'ium'
'unununium'

段落截取(三):

Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one:

>>> word = 'Python'
>>> word[0]  # character in position 0 这是注释
'P'
>>> word[5]  # character in position 5 这是注释
'n'

名词解释

  1. index 索引值
    还记得我们举例说列表就是一栋大楼吗?字符串是特殊的列表(它每层的东西是不能够改变的)。所以,索引其实就是楼号,不过是从零开始数的。
  2. subscripted 下标
    下标就是索引值。他们是一样的。

段落大意

字符串能够用索引取值。
python没有单一字符类型,单独的字母也叫字符串,只不过他只有一个索引值。


段落截取(四):

Indices may also be negative numbers, to start counting from the right

>>> word = 'Python'
>>> word[-1]  # last character
'n'
>>> word[-2]  # second-last character
'o'
>>> word[-6]
'P'

名词解释

negative numbers
负数

段落大意

索引也可以用负数从右边开始数。不过起始值是-1

注:正负只是表示数数的方向是从右往左还是从左往右,不要把数学中的坐标概念混进去。


段落截取(五)

In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain substring:

>>> word = 'Python'
>>> word[0:2]  # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5]  # characters from position 2 (included) to 5 (excluded)
'tho'
>>> word[:2]   # character from the beginning to position 2 (excluded)
'Py'
>>> word[4:]   # characters from position 4 (included) to the end
'on'
>>> word[-2:]  # characters from the second-last (included) to the end
'on'

名词解释

slice
中文翻译:一般会翻译为分片操作。
其实就是切段操作。

段落大意

索引支持分片操作

注意分片操作的几个形式:

  1. 方括号
    表示下标
  2. 分号
    表示分片操作
  3. 范围
    包含左值,不包含右值。(详情看代码中的注释)
  4. 范围可以是空值(参考上例代码)

字符串基础知识就是这些,但是其实还有很多细节,我们没有办法一一讲到,甚至有些细节对初学者是非常不友好的。所以我把他们省略了。
如果你想提升自己,试试自己阅读文档中我没有提到的部分。
比如: raw strings 、三引号

上一篇 下一篇

猜你喜欢

热点阅读