1. 程式人生 > >Python簡明教程:基本概念

Python簡明教程:基本概念

python

1 字面意義上的常量,如2、‘This is ok‘這樣的字符串

>>> print(‘a,2,3‘)

a,2,3


2 字符串

  • 單引號(‘)

    使用單引號指示字符串,類似shell中的強引用,所有的空格、制表符照原樣保留。

>>> print(‘This is ok‘)

This is ok


  • 雙引號(“)

    在雙引號中的字符串與單引號中的字符串使用完全相同

>>> print("This is ok")

This is ok


  • 三引號(‘‘‘)

    利用三引號,可以指示一個多行的字符串,可以在三引號中自由的使用單引號和雙引號。

>>> ‘‘‘This is am string.This is the first line.

... This is the second line.

... "What‘s your name?" Lasked.

... He said "Bond,JamesBond."‘‘‘

‘This is am string.This is the first line.\nThis is the second line.\n"What\‘s your name?" Lasked.\nHe said "Bond,JamesBond."‘


  • 轉義符

>>> ‘what \‘s your name‘ #在單引號中實現單引號的使用

"what ‘s your name"

或使用雙引號

>>> "what‘your name"

"what‘your name"

>>> "This is \"ok\"," #雙引號中實現雙引號的引用

‘This is "ok",‘

備註:行尾單獨反斜杠,表示字符串在下一行繼續,如:

>>> ‘This is ok\

... ,hello‘

‘This is ok,hello‘


  • 自然字符串

    如果不需要轉義符那樣特別處理的字符串,那麽可以給字符串加上前綴r或R來指定一個自然字符串

>>> r‘New line are by \n‘

‘New line are by \\n‘



本文出自 “一萬年太久,只爭朝夕” 博客,請務必保留此出處http://zengwj1949.blog.51cto.com/10747365/1925662

Python簡明教程:基本概念