Python中的字符串

  1 '''
  2 python3中的字符串
  3 
  4 '''
  5 'college' '12' 'true' 'st"ude"nt'    # 单引号包围的字符串
  6 "student" "id" "116000" "st'ude'nt"  # 双引号包围的字符串
  7 
  8 '''                                  # 三引号包围的字符串
  9 单引号和双引号包围的是单行字符串
 10 "二者的作用相同"
 11 三引号包括的是多行字符串
 12 
 13 '''
 14 # 转义字符
 15 x='\000\101\102'
 16 y='\000\x61\x63'
 17 x,y
 18 ('\x00AB','\x00ac')
 19 print(x,y)  # ?这里有问题,没搞懂
 20 print("Python\n语言\t程序\tDesign")
 21 
 22 #字符串的格式化
 23 '''
 24 %操作符格式化字符串的模板格式
 25 
 26 %[(name)[flags][width].[precision]]typecode
 27        name:可选参数,当需要格式化的值为字典类型时,用于指定字典的key
 28        flags:可选参数,可供选择的值如下
 29                +:
 30                -:
 31                空格:
 32                0:
 33        width:可选参数,指定格式字符串的占用宽度
 34        precision:可选参数,指定数值型数据保留的小数位数
 35        typecode:必选参数,指定格式控制符
 36 '''
 37 # 用%操作符格式化字符串
 38 '''
 39     %c
 40     %s
 41     %d
 42     %f
 43     %e
 44 '''
 45 print("%d %d"%(12,12.3))
 46 # 输出:12 12
 47 print("%5s  is  %-3d  years  old"%("Rose",18))
 48 # 输出: Rose  is  18   years  old
 49 
 50 # formate()方法
 51 
 52 # 位置参数
 53 print("{} is {} years old".format("Rose",18))
 54 # 输出:Rose is 18 years old
 55 print("{0} is {1} years old".format("Rose",18))
 56 # 输出:Rose is 18 years old
 57 print("Hi,{0}!{0} is {1} years old".format("Rose",18))
 58 # 输出:Hi,Rose!Rose is 18 years old
 59 
 60 # 关键字参数
 61 '''
 62 待补充
 63 
 64 '''
 65 
 66 # 下标参数
 67 '''
 68 待补充
 69 
 70 '''
 71 
 72 # 模板字符串str的格式控制
 73 '''
 74 [[fill]align][sign][width][,][.precision][type]
 75 
 76 '''
 77 
 78 
 79 # 字符串的操作符
 80 '''
 81     + :连接左右两端的字符串。
 82 
 83   *  :重复输出字符串。
 84 
 85   [ ] :通过索引获取字符串中的值。
 86 
 87   [start:stop:step]:开始,结束位置的后一个位置,步长。
 88 
 89   in  :判断左端的字符是否在右面的序列中。
 90 
 91   not in:判断左端的字符是否不在右面的序列中。
 92 
 93   r/R :在字符串开头使用,使转义字符失效。
 94 
 95 '''
 96 # 实例
 97 # 字符串使用 + 号
 98 strs = "hello " + "world."
 99 print(strs)
100 # hello world.
101 
102 # 字符串使用 * 号
103 strs = 'abc '
104 # 无论数字在哪一端都可以
105 print(3*strs)
106 # abc abc abc
107 
108 print(strs * 3)
109 # abc abc abc
110 
111 # 使用索引下标
112 strs = "hello world."
113 print(strs[4])
114 # o
115 print(strs[7])
116 # o
117 
118 # 切片操作,左闭右开原则
119 strs = "hello world."
120 # 将字符串倒序输出
121 print(strs[::-1])
122 # .dlrow olleh
123 
124 print(strs[6:11:])
125 # world
126 
127 strs = "ABCDEFG"
128 print("D" in strs)
129 # True
130 print("L" in strs)
131 # False
132 
133 print("D" not in strs)
134 # False
135 print("L" not in strs)
136 # True
137 
138 # 使用 r 使字符串中的转义字符失效
139 print('a\tb')
140 # a    b
141 print(r'a\tb')
142 # a\tb
143 
144 # 输入语句
145 varname= input("promptMessage")
146 
147 #输出语句
148 '''
149 print()函数的基本格式:
150 print([obj1,...])[,sep=' '][,end='\n'][,file=sys.stdout])
151 
152 '''

 

posted @ 2021-03-07 22:36  呆贝斯  阅读(98)  评论(0编辑  收藏  举报