首页 > 技术文章 > Python 字符串

fag888 2015-12-28 13:21 原文

Python字符串:
在Python中的字符串被确定为一组连续的字符在引号之间。 Python允许在任何对单引号或双引号。串的子集,可以使用切片操作符可采用([]和[:]),索引从0开始的字符串的开始和结束(-1)。

加号(+)符号的字符串连接操作符,而星号(*)表示重复操作。例如:

#!/usr/bin/python

str = 'Hello World!'

print str          # Prints complete string
print str[0]       # Prints first character of the string
print str[2:5]     # Prints characters starting from 3rd to 5th
print str[2:]      # Prints string starting from 3rd character
print str * 2      # Prints string two times
print str + "TEST" # Prints concatenated string

这将产生以下结果:

Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST“

推荐阅读