sqlserver 字符串中添加单引号_Python3 教程-- 7、字符串
生活随笔
收集整理的這篇文章主要介紹了
sqlserver 字符串中添加单引号_Python3 教程-- 7、字符串
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python 字符串
除了數字,Python也能操作字符串。字符串有幾種表達方式,可以使用單引號或雙引號括起來:
>>> 'spam eggs''spam eggs'>>> 'doesn't'"doesn't">>> "doesn't""doesn't">>> '"Yes," he said.''"Yes," he said.'>>> ""Yes," he said."'"Yes," he said.'>>> '"Isn't," she said.''"Isn't," she said.'Python中使用反斜杠轉義引號和其它特殊字符來準確地表示。
如果字符串包含有單引號但不含雙引號,則字符串會用雙引號括起來,否則用單引號括起來。對于這樣的輸入字符串,print() 函數會產生更易讀的輸出。
跨行的字面字符串可用以下幾種方法表示。使用續行符,即在每行最后一個字符后使用反斜線來說明下一行是上一行邏輯上的延續:
以下使用 來添加新行:
>>> '"Isn't," she said.''"Isn't," she said.'>>> print('"Isn't," she said.')"Isn't," she said.>>> s = 'First line.Second line.' # 意味著新行>>> s # 不使用 print(), 包含在輸出中'First line.Second line.'>>> print(s) # 使用 print(), 輸出一個新行First line.Second line.以下使用 反斜線() 來續行:
hello = "This is a rather long string containingseveral lines of text just as you would do in C. Note that whitespace at the beginning of the line is significant."print(hello)注意,其中的換行符仍然要使用 表示——反斜杠后的換行符被丟棄了。以上例子將如下輸出:
This is a rather long string containingseveral lines of text just as you would do in C. Note that whitespace at the beginning of the line is significant.或者,字符串可以被 """ (三個雙引號)或者 ''' (三個單引號)括起來。使用三引號時,換行符不需要轉義,它們會包含在字符串中。以下的例子使用了一個轉義符,避免在最開始產生一個不需要的空行。
print("""甥慳敧: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to""")其輸出如下:
Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to如果我們使用"原始"字符串,那么 不會被轉換成換行,行末的的反斜杠,以及源碼中的換行符,都將作為數據包含在字符串內。例如:
hello = r"This is a rather long string containingseveral lines of text much as you would do in C."print(hello)將會輸出:
This is a rather long string containingseveral lines of text much as you would do in C.字符串可以使用 + 運算符串連接在一起,或者用 * 運算符重復:
>>> word = 'Help' + 'A'>>> word'HelpA'>>> ''''兩個緊鄰的字面字符串將自動被串連;上例的第一行也可以寫成 word = 'Help' 'A' ;這樣的操作只在兩個字面值間有效,不能隨意用于字符串表達式中:
>>> 'str' 'ing' # >> 'str'.strip() + 'ing' # >> 'str'.strip() 'ing' #總結
以上是生活随笔為你收集整理的sqlserver 字符串中添加单引号_Python3 教程-- 7、字符串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python语言编写的modbus协议_
- 下一篇: 外部接口需求怎么写_软件需求规约怎么写