今天在班群里老師發了python面試題 第一題就是簡述python單引號、雙引號、三引號的場景和區別,根據以往經驗以及從網上找到的資源,筆者自己整理了一下;
1 單引號和雙引號的用法是類似的,但是如果字符串里有相同的符號時要進行轉義(\)
eg1:
1) print 'hello'
2) print "hello"
輸出結果都是hello
3) print 'hell\'o'
4) print "hell'o"
3和4,結果都是hell'o
5) print 'hell"o'
6) print "hell\"o"
5和6,結果都是hell"o
2. 三引號與他們不同之處:“所見即所得”,對於一段html或xml格式的字符串時用三引號更直觀點(此時要用單引號或雙引號時候需要進行轉義)
如:
print '''
hello'
world"
'''
結果是
hello'
world"
