1、单引号字符串和转义引号python
当字符串中出现单引号'时,咱们能够用双引号""将该字符串引发来:"Let's go!"shell
而当字符串中出现双引号时,咱们能够用单引号''将该字符串引发来:' "Hello,world!" she said 'ide
可是当字符串中又有单引号'又有双引号"时该如何处理呢:使用反斜线(\)对字符串中的引号进行转义:'Let\'s go!'函数
2、字符串spa
>>>"Let's say" ' "Hello,world!" ' 'Let\'s say "Hello,world!" '
>>>x="hello,"
>>>y="world!"
>>>x y
SyntaxError: invalid syntax
>>>"hello,"+"world!"
'hello,world!'
>>>x+y
'hello,world!'
上面只是一个接着一个的方式写了两个字符串,Python就会自动拼接它们,可是若是赋值给变量再用这种方式拼接则会报错,由于这仅仅是书写字符串的一种特殊方法,并非拼接字符串的通常方法;这种机制用的很少。用"+"好能够进行字符串的拼接;code
2.字符串表示,str和reprblog
>>>print repr("hello,world!") 'hello,world!' >>>print repr(10000L) 10000L >>>print str("Hello,world!") Hello,world! >>>print str(10000L) 10000
str和int、bool同样,是一种类型,而repr仅仅是函数,repr(x)也能够写做`x`实现(注意,`是反引号,不是单引号);不过在Python3.0中已经再也不使用反引号了。所以,即便在旧的代码中应该坚持使用repr。字符串
3.input和raw_input的比较input
input会假设用户输入的是合法的Python表达式,好比输入数值1,程序不会看成是str,而是看成int类型,输入x,程序会看成用户输入的是变量x,若是输入"x",程序才会人但是字符串;string
raw_input函数它会把全部的输入看成原始数据,而后将其放入字符串中。
>>> name=input("what is your name ?");print name what is your name ?Allen Traceback (most recent call last): File "<pyshell#22>", line 1, in <module> name=input("what is your name ?");print name File "<string>", line 1, in <module> NameError: name 'Allen' is not defined >>> name=input("what is your name ?");print name what is your name ?"Allen" Allen >>>input("Enter a number:") Enter a number:3 3 >>>raw_input("Enter a number:") Enter a number:3 '3'
四、长字符串
若是须要写很是长的字符串,它须要跨多行,那么可使用三个引号代替普通引号。
1 print '''This is very long string. 2 It continues here . 3 And it's not over yet. 4 Still here.''' 5 #也可使用三个双引号,例如: 6 """Like This"""
由于这种不同凡响的引用方式,你能够在字符串之间同时使用单引号和双引号,而不须要使用反斜线进行转义。
普通字符串也能够跨行。若是一行之中最后一个字符是反斜线,那么换行符自己"转义"了,也就是被忽略了,例如:
print "Hello,\ world!" #上句会打印Hello,world!。这个用法也适用于表达式和语句: >>>1+2+\ 4+5 12 >>>print \ 'Hello,world' Hello,world
五、原始字符串
\反斜线有转义的功能,\n表示换行符,若是打印一个路径,例如:
>>>print 'C:\nowhere' #打印结果以下 C: owhere
#咱们能够经过反斜线进行转义,改成:
>>>print 'C:\\nowhere'
可是若是对于长路径,那么须要不少的反斜线,这样原始字符串就派上用场了。
原始字符不会把反斜线看成特殊字符串。
>>>print r'C:\nowhere' C:\nowhere >>>print r'C:\Program Files\fnord\foo\bar\baz\frozz\bozz' C:\Program Files\fnord\foo\bar\baz\frozz\bozz
固然咱们也要像日常同样对引号进行转义,可是,最后的输出的字符串也包含了转义所用的反斜线
>>> print r'Let's go SyntaxError: invalid syntax >>> print r'Let\'s go' Let\'s go
可是不能在原始字符串结尾输入反斜线。
print r"This is illegal\"
上面写法会报错,参照上一个范例这是一个显而易见的结论。最后一个字符是反斜线,Python就不清楚是否应该结束字符串。
但若是字符串最后一个字符确实是\,可使用一个技巧解决上述问题
print r'C:\Program Files\foo\bar' '\\'
C:\Program Files\foo\bar\
六、Unicode字符串
Pyhon 中的普通字符串在内部是以8位的ASCII码造成存储的,而Unicode字符串则存储为16位Unicode字符,这样就可以表示更多的字符集了,包括世界上大多数语音的特殊字符,例子:
>>>u'Hello,world!' u'Hello,world!'
能够看到,Unicode字符串使用u前缀,就像原始字符串使用r同样。