Python基础语法——(引号、字符串、长字符串、原始字符串、Unicode)

一、单引号字符串和转义引号

  当字符串中出现单引号'时,我们可以用双引号""将该字符串引起来:"Let's go!"

  而当字符串中出现双引号时,我们可以用单引号''将该字符串引起来:' "Hello,world!" she said '

  但是当字符串中又有单引号'又有双引号"时该如何处理呢:使用反斜线(\)对字符串中的引号进行转义:'Let\'s go!'

二、字符串

  1. 拼接字符串
>>>"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就会自动拼接它们,但是如果赋值给变量再用这种方式拼接则会报错,因为这仅仅是书写字符串的一种特殊方法,并不是拼接字符串的一般方法;这种机制用的不多。用"+"好可以进行字符串的拼接;

  2.字符串表示,str和repr

>>>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会假设用户输入的是合法的Python表达式,比如输入数值1,程序不会当作是str,而是当作int类型,输入x,程序会当作用户输入的是变量x,如果输入"x",程序才会人可是字符串;

  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'

 4、长字符串

如果需要写非常长的字符串,它需要跨多行,那么可以使用三个引号代替普通引号。

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"""
LongStr

因为这种与众不同的引用方式,你可以在字符串之间同时使用单引号和双引号,而不需要使用反斜线进行转义。

普通字符串也可以跨行。如果一行之中最后一个字符是反斜线,那么换行符本身"转义"了,也就是被忽略了,例如:

print "Hello,\
world!"
#上句会打印Hello,world!。这个用法也适用于表达式和语句:
>>>1+2+\
            4+5
12
>>>print \
            'Hello,world'
Hello,world

5、原始字符串

\反斜线有转义的功能,\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\

6、Unicode字符串

Pyhon 中的普通字符串在内部是以8位的ASCII码形成存储的,而Unicode字符串则存储为16位Unicode字符,这样就能够表示更多的字符集了,包括世界上大多数语音的特殊字符,例子:

>>>u'Hello,world!'
u'Hello,world!'

可以看到,Unicode字符串使用u前缀,就像原始字符串使用r一样。

 

posted @ 2017-05-10 00:17  Allen6167  阅读(6688)  评论(0编辑  收藏  举报