下面我們來了解一下python的字符串的表示方法
python有三種字符表示,分別爲單引號,雙引號,三個雙引號
一、單引號和雙引號的區別
在C中單引號表示單個字符,雙引號表示字符串
char a='a'; char b[10]="Hello!"
單在python中,可以這樣表示
str1='You are great!'
str2="I love python"
也就是說單引號和雙引號都可以表示字符串???
如果你遇到下面的問題呢?
You're great!
I love "python"
在C中這樣表示"You\'re great!","I love \"python\""
如果一個字符串裏有很多的單引號和雙引號,你會不會很煩?
python中這樣表示"You're great!",'I love "python"',是不是很方便?如果一個字符串裏既又單引號又有雙引號怎麼辦?例如上面的
I don't love "python"
這。。。看來還是沒有把問題解決完全,但是python團隊已經盡力了,咱們就別爲難它了,呵呵
二、三個雙引號的用法
如果要把幾行字符串保存到一個變量中,如下:
str3="Hey! \n\
I'm from Harbin industrial universary!\n\
Where are you from?"
注意到裏面的‘\n’, '\'是行連接符
你也可以這樣寫
str3="""Hey!
I'm from Harbin industrial universary!
where are you from?"
也就是可以分行寫,回車也會算進去。