stringpython
Ⅰ python怎麼輸出string
字元串靜態拼接
print 'a' 'b'
=> ab
用空格分隔,並在最後追加換行符
print 'a', 'b'
=> a b
用空格分隔,不在最後追加換行符
print 'a', 'b',
=> a b
列印元組 ('a', 'b') 的 str 形式,並在最後追加換行符
print('a', 'b'),
=> ('a', 'b')
和上個等價
print('a', 'b',)
=> ('a', 'b')
列印元組 ('a', 'b') 的 str 形式,不在最後追加換行符
print('a', 'b'),
=> ('a', 'b')
不在最後追加換行符
print('hello'),
=> hello
----------------------------------------
列印到標准錯誤流
import sys
print >> sys.stderr, 'spam'
sys.stderr.write('spam\n')
from __future__ import print_function
print('spam', file = sys.stderr)
----------------------------------------
列印到文件流
logfile = open('/tmp/mylog.txt', 'a')
print >> logfile, 'Fatal error: invalid input!'
logfile.close()
Ⅱ string是python關鍵字嗎
不是。string不是python的關鍵字,String是一個Unicode字元序列,是Python中最重要的數據類型之一,可以使用單引號、雙引號、三引號創建創建一個字元串,Python不支持單字元類型,也就是沒有字元只有字元串。
Ⅲ python中string(.)是什麼意思
string(.)是語法錯誤。string. 表示調用標准庫裡面的string模塊的方法。
Ⅳ Python String和PyQt QString的區別
以下在python2.5和PyQt4.4.6 for python2.5環境下討論。
在python中有兩種與字元有關的類型:string object和Unicode object。
平時進行輸入輸出的一般都用string
object,當需要顯示一些特殊字元或者中文等文字時候,需要轉換為Unicode編碼。在PyQt中也有兩種字元類型與上面兩者對應:QByteArray和QString,主要是使用QString操作數據。
1) python string
object可以理解為一個接一個位元組的位元組組,至於表示什麼編碼,與表示文字有關,比如「python
string」,「中文」。注意它是有不同編碼區分的。
PyQt中與之對應的是QbyteArray,而不是Qstring。
A built-in string object (plain or Unicode) is a sequence of
characters used to store and represent text-based information
(plain strings are also sometimes used to store and represent
arbitrary sequences of binary bytes). (摘自《Python in a
NutShell》)
QByteArray can be used to store both raw bytes (including '"0's)
and traditional 8-bit '"0'-terminated.(摘自《PyQt手冊》)
2)Python Unicode
object可以理解為固定使用utf-16編碼的位元組組,其中英文和中文都使用兩個位元組(16位)來表示,如:u"Python
Unicode object"、u"中文"。
PyQt中與之對應的就是QString了。
Unicode string literals have the same syntax as other string
literals, with a u or U immediately before the leading quote.
(摘自《Python in a NutShell》)
Qt also provides the QString class to store string data. It stores
16-bit Unicode characters, making it easy to store
non-ASCII/non-Latin-1 characters in your
application.(摘自《PyQt手冊》)
QString stores a string of 16-bit QChars, where each QChar
corresponds one Unicode 4.0 character.(摘自《PyQt手冊》)
2 PyQt內部類型轉換
QString有
toAscii()、toUtf8()函數轉換為QByteArray類型,(這個基本不用,因為很少直接用QByteArray類型)有__init__
(self, QByteArray a)函數將QByteArray類型轉為QString。
3. Python string object和Python Unicode object相互轉換
1)Python string object是原始編碼是有區分的,通過 decode('原始編碼')
函數解碼得到通用utf16編碼即Python Unicode object。
>>>"python
string".decode('ascii')
或者
>>>"python
string".decode()
得到 u"python string"
因為默認按ascii解碼。
>>>"中文".decode('gbk')
得到 u""u4e2d"u6587" ,列印出來就是 中文 二字。(注意結果是2位元組一組,共兩組,對應兩個漢字)
又:"python string".decode('gkb') ,即按漢字來解碼,也可以得到 u"python
string",因為gbk編碼也支持英文字母;
但是"中文".decode('ascii') 即按ascii解碼是錯誤的,因為ascii編碼不支持漢字!
>>>
"dfdf".decode()
u'dfdf'
>>>
"dfdf".decode("ascii")
u'dfdf'
>>>
"dfdf".decode("gbk")
u'dfdf'
>>>
"中文".decode("gbk")
u'"u4e2d"u6587'
>>>print
"中文".decode("gbk")
中文
>>>
"中文".decode("gb2312")
u'"u4e2d"u6587'
>>>
"中文".decode("ascii")
Traceback (most recent call last):
File "<interactive input>", line 1,
in <mole>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd6 in
position 0: ordinal not in range(128)
2)Python Unicode object原始編碼固定是utf16,通過 encode('目的編碼') 編碼來得到Python
string object。
>>>u"unicode
string".encode()
或者
>>>u"unicode
string".encode('ascii')
得到
'unicode string',默認目的編碼為ascii。
>>>u"中文".encode("gbk")
得到'"xd4"xd0"xce"xc4',列印出來就是 中文。(注意結果是1位元組一組,共4組)
>>>
u"sdff".encode()
'sdff'
>>>
u"sdff".encode('ascii')
'sdff'
>>>
u"sdff".encode('gbk')
'sdff'
>>>
u"sdff".encode('gb2312')
'sdff'
>>>
u"中文".encode('gbk')
'"xd6"xd0"xce"xc4'
>>> print
u"中文".encode('gbk')
中文
>>>
u"中文".encode('ascii')
Traceback (most recent call last):
File "<stdin>", line 1, in
<mole>
UnicodeEncodeError: 'ascii' codec can't encode characters in
position 0-1: ordin
al not in range(128)
注意:執行>>>
u"中文".encode('gbk')命令需要你的IDE支持gbk編碼,在官方shell下執行肯定沒問題,但如果你的IDE比如PyWin中文輸入異常,則可能報錯。
4. Python string object和Python Unicode object向QString的轉換。
Qt一般不直接操作QByteArray,只需關注Python string object和Python Unicode
object向QString的轉換。
很多關於PyQt4的英文書籍說:PyQt函數需要QString參數的地方都可以直接用Python string
object或者Python Unicode object,如果非要轉換可以直接用QtCore.QString()構造。比如《GUI
Programming with PyQt》,再如《PyQt手冊》:
Whenever PyQt expects a QString as a function argument, a Python
string object or a Python Unicode object can be provided instead,
and PyQt will do the necessary conversion automatically.
You may also manually convert Python string and Unicode objects to
QString instances by using the QString constructor as demonstrated
in the following code fragment:
qs1 = QtCore.QString("Converted Python string object")
qs2 = QtCore.QString(u"Converted Python Unicode object")
但可惜這只適用於英文即ascii編碼,對於中文則行不通!
直接的QString:
>>>
QtCore.QString('中文')
PyQt4.QtCore.QString(u'"xd6"xd0"xce"xc4')
>>> print
QtCore.QString('中文')
Traceback (most recent call last):
File "<stdin>", line 1, in
<mole>
UnicodeEncodeError: 'ascii' codec can't encode characters in
position 0-3: ordin
al not in range(128)
>>>
>>>
QtCore.QString(u'中文')
PyQt4.QtCore.QString(u'"u4e2d"u6587')
>>> print
QtCore.QString(u'中文')
Traceback (most recent call last):
File "<stdin>", line 1, in
<mole>
UnicodeEncodeError: 'ascii' codec can't encode characters in
position 0-1: ordin
al not in range(128)
>>>
因為它們都是默認按ascii編碼轉換!
GUI編程:
可以創建一個QTextEdit對象myTextEdit, 檢驗:
myTextEdit.append("中文")
或者
myTextEdit.append(u"中文")
或者
myTextEdit.append(QtCore.QString('中文'))
或者
myTextEdit.append(QtCore.QString(u'中文'))
你會發現顯示都是亂碼...因為它們都是默認按ascii編碼進行內部轉換得到QString相應utf16編碼的。
解決方法是:
利用unicode()函數顯示指定gb2312編碼進行中文編碼轉換,轉換後的Python Unicode
object則是可以直接作為QString參數代入用的:
>>> unicode('中文',
'gb2312', 'ignore')
u'"u4e2d"u6587'
>>> print
unicode('中文', 'gb2312', 'ignore')
中文
>>>
myTextEdit.append(unicode('中文', 'gb2312', 'ignore'))
#用以替代myTextEdit.append(u"中文")
或者多此一舉下:
myTextEdit.append(QtCore.QString(unicode('中文', 'gb2312',
'ignore')))
#用以替代myTextEdit.append(QtCore.QString(u'中文'))
5. QString向Python string object和Python Unicode object的轉換。
Python中需要用Python string object和Python Unicode
object的地方可就不一定可以直接用QString了!!!
QString向Python string object轉換可以理解,因為編碼不同。
QString向Python Unicode object的轉換?需要轉換嗎?不都是utf16編碼嗎?
QString是tuf16編碼,但是它的實現並非Python Unicode
object那樣直接的utf16碼,而實際是一個QChar串,每個QChar才對應unicode符,所以地位相當但並不相同。
許多英文書籍寫到:可以使用str()函數直接將QString轉換為Python string
object,可以使用unicode()直接將QString轉換為Python Unicode
object。如《PyQt手冊》:
In order to convert a QString to a Python string object use the
Python str() builtin. Applying str() to a null QString and an empty
QString both result in an empty Python string object.
In order to convert a QString to a Python Unicode object use the
Python unicode() builtin. Applying unicode() to a null QString and
an empty QString both result in an empty Python Unicode
object.
但同樣只適用於英文,具體見下面分別分析。
1)QString向Python Unicode object的轉換。
>>> from PyQt4 import
QtGui, QtCore
>>>
unicode(QtCore.QString('def'))
u'def'
>>> print
unicode(QtCore.QString('def'))
def
對於中文,unicode()必須要指定編碼後有效。(這樣也只針對直接的QString有效?對於Qt
GUI編程中,從QWidget取得的QString無效?)
>>> from PyQt4 import
QtGui, QtCore
>>>
unicode(QtCore.QString('中文'))
u'"xd6"xd0"xce"xc4'
>>> print
unicode(QtCore.QString('中文'))
Traceback (most recent call last):
File "<stdin>", line 1, in
<mole>
UnicodeEncodeError: 'gbk' codec can't encode character u'"xd6' in
position 0: il
legal multibyte sequence
指定原始編碼後:
>>>
unicode(QtCore.QString('中文'),'gbk','ignore')
u'"u4e2d"u6587'
>>> print
unicode(QtCore.QString('中文'),'gbk','ignore')
中文 TEST
Ⅳ python中如何把string 轉換成int
用數字字元串初始化int類,就可以將整數字元串(str)轉換成整數(int):
In [1]: int(『1234』)
Out[1]: 1234
相反用整數初始化str類,就可以將整數(int)轉換為對應的字元串(str):
In [2]: str(1234)
Out[2]: 『1234』
如果字元串是浮點數,可以用字元串初始化float類,把浮點數字元串(str)轉換成浮點數(float):
In [3]: float(『12.34』)
Out[3]: 12.34
(5)stringpython擴展閱讀:
Python (英國發音:/ˈpaɪθən/ 美國發音:/ˈpaɪθɑːn/), 是一種面向對象的解釋型計算機程序設計語言,由荷蘭人Guido van Rossum於1989年發明,第一個公開發行版發行於1991年。
Python是純粹的自由軟體,源代碼和解釋器CPython遵循 GPL(GNUGeneral Public License)許可。Python語法簡潔清晰,特色之一是強制用空白符(white space)作為語句縮進。
Python具有豐富和強大的庫。它常被昵稱為膠水語言,能夠把用其他語言製作的各種模塊(尤其是C/C++)很輕松地聯結在一起。常見的一種應用情形是,使用Python快速生成程序的原型(有時甚至是程序的最終界面),然後對其中有特別要求的部分,用更合適的語言改寫,比如3D游戲中的圖形渲染模塊,性能要求特別高,就可以用C/C++重寫,而後封裝為Python可以調用的擴展類庫。需要注意的是在您使用擴展類庫時可能需要考慮平台問題,某些可能不提供跨平台的實現。
7月20日,IEEE發布2017年編程語言排行榜:Python高居首位 。
2018年3月,該語言作者在郵件列表上宣布 Python 2.7將於2020年1月1日終止支持。用戶如果想要在這個日期之後繼續得到與Python 2.7有關的支持,則需要付費給商業供應商。
Ⅵ python string 里怎麼有沒有類似substr的方法
String在python中被當做Unicode代碼值的序列,用string[i:j:k]格式來進行切片。Unicode值的取值范圍在U0000 - U10FFFF之間。Python沒有char類型,字元串中的每個Unicode代碼都表示為長度為1的String對象。
序列切片的格式為string[i:j:k],i為從0開始的起始位置;j為終止位置;k表示切片的長度。其中的j和k可以省略,j和k都省略,表示取i位置的一個字元;只省略k,就是類似substr的取子串的功能。下面是幾個例子:
>>>a=""
>>>a[0]
'A'
>>>a[1]
'B'
>>>a[0:5]
'ABCDE'
>>>a[1:5]
'BCDE'
>>>a[0:20:0]
Traceback(mostrecentcalllast):
File"<stdin>",line1,in<mole>
ValueError:slicestepcannotbezero
>>>a[0:20:1]
'ABCDEFGHIJABCDEFGHIJ'
>>>a[0:20:2]
'ACEGIACEGI'
>>>a[0:20:3]
'ADGJCFI'
>>>
Ⅶ python string函數
python string函數是什麼?一起來看下吧:
python string函數包括:
1、str.capitalize:將原字元串內的首字母轉成大寫,其他部分小寫,再返回新字元串
print("s.capitalize() = {function}" s.capitalize() = Abcada a
2、str.lower:將原字元串的字母轉為小寫
print("s.lower() = {function}".format(function = s.lower())) s.lower() = abcada a
3、str.upper:將原字元串的字母轉為大寫
print("s.upper() = {function}".format(function = s.upper())) s.upper() = ABCADA A
4、str.swapcase:將原字元串的大寫小寫反轉
print("s.swapcase() = {function}".format(function = s.swapcase())) s.swapcase() = ABCAdA A
5、str.title:原字元串內如果有特殊字元(包括數字)連接字母,則將特殊字元後的首個英文字母轉化為大寫形態,並返回新字元串
print("s2.title() = {function}".format(function = s2.title())) s2.title() = 123A Abc Abcsaa S
6、str.center:str.center(寬度,填充字元) 將字元串以居中的格式返回,若寬度值比len(s)小則返回原字元串,填充以從左到右為規則,填充字元的默認值為空格,值可以自己更改
print("s2.center() = {function}".format(function = s2.center(19,'&'))) print("s2.center() = {function}".format(function = s2.center(20,'&'))) #s2 = 123a abc ABCSAa s s2.center() = &123a abc ABCSAa s s2.center() = &123a abc ABCSAa s &
7、str.expandtabs:str.expandtabs(tabsize = 8) 將原字元串中 以前的字元補滿8位(默認),tabsize的值從0-7即8位,在0-7中任意取值則默認tabsize = 8,此後往上+1,就相當於增加一個空格
print("s3.expandtabs ={function}".format(function = s3.expandtabs())) print("s3.expandtabs ={function}".format(function = s3.expandtabs(0))) print("s3.expandtabs ={function}".format(function = s3.expandtabs(5))) print("s3.expandtabs ={function}".format(function = s3.expandtabs(8))) print("s3.expandtabs ={function}".format(function = s3.expandtabs(9)))#s3 = " as b123" s3.expandtabs = as b123 s3.expandtabs =as b123 s3.expandtabs = as b123 s3.expandtabs = as b123 s3.expandtabs = as
除了上述舉例的,string函數還有許多實用的函數。
Ⅷ Python的string前面加上『r』,
在Python的string前面加上『r』, 是為了告訴編譯器這個string是個raw string,不要轉意backslash '' 。 例如,\n 在raw string中,是兩個字元,\和n, 而不會轉意為換行符。由於正則表達式和 \ 會有沖突,因此,當一個字元串使用了正則表達式後,最好在前面加上'r'。
例:r"\n\n\n\n\n\n」
作用:聲明後面的字元串是普通字元串
特殊字元串中含有: 轉義字元 \n \t 什麼什麼的
用途:一般用在 正則表達式、文件絕對地址
1,正則表達式:
2、系統路徑
這樣就不用專門的去處理引號之中的特殊字元了