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
热门推荐
10 朋友孩子考试祝福语简短
11 浪漫的生日祝福语简短
12 婶婶生日贺词简短祝福语
13 女儿结婚账单祝福语简短
14 九子订婚祝福语简短
15 老哥出门祝福语简短语
16 合字的祝福语简短
17 俄语祝福语贺卡文案简短
18 哥哥考大学祝福语简短