美文网首页
第二课 字符串

第二课 字符串

作者: 小坤老师 | 来源:发表于2016-12-08 14:34 被阅读0次
字符串任何语言的基本操作,学完可以大致了解一下语言的特色
  • 创建字符串
字符串可以用单引号 ('...') 或双引号 ("...") 标识
>>> 'hello world'
'hello world'
>>> "hello world"
'hello world'
>>> 
——————————————
(\)可以用来转义引号
>>> 'he says "hello world"'
'he says "hello world"'
>>> "he says \"hello world\""
'he says "hello world"'
>>> 
——————————————
print()为输出语句    #print和()之间空格可加可不加
>>> print ('he says \"hello world\"')
he says "hello world"
>>> 
>>> s = "he says \"hello world\""
>>> print(s)
he says "hello world"
>>> 

如果带有 \ 的字符被当作特殊字符,可以使用 *原始字符串*,方法是在第一个引号前面加上一个 r
>>> print('ni\'hao\n')
ni'hao

>>> print(r'ni\'hao\n')
ni\'hao\n
>>> 

字符串文本能够分成多行。
一种方法是使用三引号:"""..."""或者 '''...'''。行尾换行符会被自动包含到字符串中,但是可以在行尾加上 \ 来避免这个行为。
>>> print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")
将生成以下输出(注意,没有开始的第一行):
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to

>>>
  • 字符串操作语句

‘+’

1.字符串可以由 + 操作符连接(粘到一起),可以由 * 表示重复
>>> print(3*'ni'+'shi')
nininishi
>>> print(3*('ni'+'shi'))
nishinishinishi

'*'

2.* 只用于两个字符串文本,不能用于字符串表达式
>>> s = 'ni'
>>> pirnt(3*s)
Traceback (most recent call last):
  File "<pyshell#56>", line 1, in <module>
    pirnt(3*s)
NameError: name 'pirnt' is not defined

‘+’使用注意事项

1.+可以用于两个字符串文本
>>> n = 'shi'
>>> print(s+n)
nishi
>>> print(s,n)
ni shi>>> print(3*'ni'+'shi')
nininishi
>>> print(3*('ni'+'shi'))
nishinishinishi
>>> s = 'ni'
>>> pirnt(3*s)
Traceback (most recent call last):
  File "<pyshell#56>", line 1, in <module>
    pirnt(3*s)
NameError: name 'pirnt' is not defined
>>> n = 'shi'
>>> print(s+n)
nishi
>>> print(s,n)
ni shi
2.连接多个变量或者连接一个变量和一个字符串文本
>>> p = 'py'
>>> print(p + 'thon')
python
>>> 
  • 切分字符串

1. 索引

字符串也可以被截取(检索)。
类似于 C ,字符串的第一个字符索引为 0 。
Python没有单独的字符类型;一个字符就是一个简单的长度为1的字符串。

>>> sentence = 'ni hao shi jie'
>>> sen = sentence
>>> sen[0]
'n'
>>> sen[10]
' '

索引也可以是负数,这将导致从右边开始计算。

>>> sen[-1]
'e'
2. 切片

索引用于获得单个字符,切片 让你获得一个子字符串。

>>> sen[0:5]
'ni ha'
>>> sen[-1:0]
''
>>> sen[-1:3]
''
>>> sen[-1:-4]
''
>>> sen
'ni hao shi jie'
>>> sen[-4:-1]
' ji'
>>> sen[0:-1]
'ni hao shi ji'
>>> sen[1:-1]
'i hao shi ji'
>>> 

注意,包含起始的字符,不包含末尾的字符。这使得 s[:i] + s[i:]永远等于 s。

>>> sen[:2]+sen[2:]
'ni hao shi jie'
>>> 

切片的索引有非常有用的默认值;省略的第一个索引默认为零,省略的第二个索引默认为切片的字符串的大小。

   +---+---+---+---+---+---+
   | P | y | t | h | o | n |
   +---+---+---+---+---+---+
   0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1

一个过大的索引值(即下标值大于字符串实际长度)将被字符串实际长度所代替;
当上边界比下边界大时(即切片左值大于右值)就返回空字符串。

字符串不可以被更改
3. 长度

内置函数返回字符串长度。

>>> s = 'supercalifragilisticexpialidocious'>>> len(s)34
4. 更多方法

Text Sequence Type — str
字符串是 序列类型 的例子,它们支持这种类型共同的操作。
String Methods
字符串和Unicode字符串都支持大量的方法用于基本的转换和查找。
String Formatting
这里描述了使用 str.format() 进行字符串格式化的信息。
String Formatting Operations
这里描述了旧式的字符串格式化操作,它们在字符串和Unicode字符串是 %
操作符的左操作数时调用。

  • 遗留问题

1.更多的字符串方法

相关文章

网友评论

      本文标题:第二课 字符串

      本文链接:https://www.haomeiwen.com/subject/mutsmttx.html