标签:
?或者三引号("""或‘‘‘)
保留字符串中的全部格式信息
1 first_name = ‘python‘ 2 print len(first_name)
输出:6
1 first_name = ‘python‘ 2 name = first_name + ‘testing‘ 3 print name
输出:pythontesting
1 name = ‘python‘ 2 print name * 3
1 name = ‘python‘ 2 print ‘x‘ in name //False 3 print ‘on‘ in name //True 4 print ‘On‘ in name //False,大小写敏感
name = ‘python‘ for c in name : print c p y t h o n
1 def vowles_count(s): 2 count = 0 3 for c in s: 4 if c in ‘aeiouAEIOU‘: 5 count += 1 6 return count 7 8 print vowles_count(‘Hello World!‘)
1 str = ‘Hello World‘ 2 3 print str[2] 4 print str[-3] 5 print str[15] 6 7 l 8 r 9 Traceback (most recent call last): 10 File "<stdin>", line 1, in <module> 11 File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile 12 execfile(filename, namespace) 13 File "C:/Users/weidan/Desktop/untitled0.py", line 12, in <module> 14 print str[15] 15 IndexError: string index out of range
1 str = ‘Hello World‘ 2 3 print str[2:6] 4 print str[4:] 5 print str[:6] 6 7 llo 8 o World 9 Hello 10 >>>
1 str = ‘Hello World‘ 2 3 print str[2:6:2] 4 print str[::-1] //获得逆字符串 5 print str[:6:2] 6 7 lo 8 dlroW olleH 9 Hlo
1 str = ‘Hello World‘ 2 str[1] = ‘a‘ 3 print str 4 5 Traceback (most recent call last): 6 File "<stdin>", line 1, in <module> 7 File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile 8 execfile(filename, namespace) 9 File "C:/Users/***/Desktop/untitled0.py", line 9, in <module> 10 str[1] = ‘a‘ 11 TypeError: ‘str‘ object does not support item assignment 12 >>>
1 str = ‘Hello World‘ 2 str = str[:1] + ‘a‘ + str[2:] 3 print str 4 5 Hallo World
标签:
原文地址:http://www.cnblogs.com/weidandan/p/4193626.html