python中输入的字怎么表示_简单讲解Python中的字符串与字符串的输入输出
字符串字符串用''或者""括起來,如果字符串內部有‘或者",需要使用\進行轉義
>>> print 'I\'m ok.'
I'm ok.
轉義字符\可以轉義很多字符,比如\n表示換行,\t表示制表符,字符\本身也要轉義,所以\\表示的字符就是\。當然如果不需要轉義,可以使用r'':
>>> print '\\\t\\'
\ \
>>> print r'\\\t\\'
\\\t\\
如果字符串內部有很多換行,用\n寫在一行里不好閱讀,為了簡化,Python允許用'''…'''的格式表示多行內容:
>>> print '''line1
... line2
... line3'''
line1
line2
line3
如果寫成程序,就是:
print '''line1
line2
line3'''
可能出現的問題
中文編碼問題
# coding = utf-8
結果報錯:
SyntaxError: Non-ASCII character ‘/xe6'
所以最后改成了
# coding=utf-8
唉....
Unicode編碼問題
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> len('中文')
6
>>> len(u'中文')
2
>>>
注意: 這個問題是由python編碼導致的,詳細的編碼問題詳見字符串和編碼,但是在python 3.x中這個編碼問題就不存在了:
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> len('中文')
2
>>> len(u'中文')
2
>>>
輸出
>>> print 'hello, world'
hello, world
>>> print 'The quick brown fox', 'jumps over', 'the lazy dog'
The quick brown fox jumps over the lazy dog
>>> print '100 + 200 =', 100 + 200
100 + 200 = 300
輸入
>>> name = raw_input()
Michael
>>> name
'Michael'
>>> print name
Michael
>>> name = raw_input('please enter your name: ')
please enter your name:
注意: raw_input返回的永遠是字符串,也就是說你輸入一個int型,返回的是一個數字字符串,你需要進行轉換:
>>> number = raw_input("輸入一個整數:")
輸入一個整數:123
>>> number
'123'
>>> number = int(raw_input("輸入一個整數:"))
輸入一個整數:123
>>> number
123
總結
以上是生活随笔為你收集整理的python中输入的字怎么表示_简单讲解Python中的字符串与字符串的输入输出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pc控制iphone的软件_如何通过“共
- 下一篇: python import 文件路径_p