當前位置:首頁 » 編程語言 » pythonstring包含

pythonstring包含

發布時間: 2023-01-30 16:06:39

1. python字元串包含幾個不同的數字

python字元串包含幾個不同的數字
字元串是由零個或多個字元組成的有限序列。而在Python 3中,它有著更明確的意思: 字元串是由Unicode碼點組成的不可變序列 (Strings are immutable sequences of Unicode code points.)

字元串是一種序列,這意味著它具備序列類型都支持的操作:

2. 在python中, string.printable :包含所有可列印字元的字元串。 什麼叫可列印字元啊菜鳥求解

ASCII碼中,第0~32號及第127號是控制字元;第33~126號是可列印字元,其中第48~57號為0~9十個阿拉伯數字;65~90號為26個大寫英文字母,97~122號為26個小寫英文字母,其餘的是一些標點符號、運算符號等

3. 用python語言,如何判斷一段字元串中是否包含指定的字元串

python的string對象沒有contains方法,不用使用string.contains的方法判斷是否包含子字元串,但是python有更簡單的方法來替換contains函數。

方法1:使用 in 方法實現contains的功能:

site = ''
if "jb51" in site:
print('site contains jb51')

輸出結果:site contains jb51

方法2:使用find函數實現contains的功能

s = "This be a string"
if s.find("is") == -1:
print "No 'is' here!"
else:
print "Found 'is' in the string."

4. 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

5. python菜鳥一問,請大大們解答,謝謝

哥們,需要import string:

>>> import string
>>> string.atoi('10') + 4
14

分析:錯誤原因說,name 'string' is not defined,意思是string沒有定義。。。所以import一下就好啦。。

6. 北大青鳥設計培訓:python編程開發環境下的軟體測試技術

軟體測試相信大家應該都不陌生了吧,今天我們就一起來了解一下,在python編程開發環境下的軟體測試都有哪些知識點。
格式化輸出我們有兩種大相徑庭地輸出值方法:表達式語句*和print()函數(三種方法使用文件對象的write()方法,標准文件輸出可以參考sys.stdout)。
通常需要對輸出做更多的格式控制,而不是簡單的列印空格分隔值。
有兩種方法可以格式化你的輸出:一種方法是由你自己處理整個字元串,通過使用字元串切片和連接操作可以創建任何你想要的輸出形式。
string類型包含一些將字元串填充到指定列寬度的有用操作。
二種方法是使用str.format()方法。
標准模塊string的Template類可以替換字元串的值。
(python標准模塊介紹-string:文本常量和模板)Python有辦法將任意值轉為字元串:repr()或str()函數。
函數str()用於將值轉化為適於人閱讀的形式,而repr()轉化為供解釋器讀取的形式(如果沒有相關語法,則會發生SyntaxError異常,沒有str()會返回與repr()等同的值。
很多類型,諸如數值或列表、字典這樣的結構,兩者解讀方式相同。
字元串和浮點數則不同。
文件讀寫通常,文件以text模式打開,這意味著,你從文件讀出和向文件寫入的字元串會被特定的編碼方式(類Linux默認是UTF-8,windows經常為gbk,容易導致一些IO問題)編碼。
模式後面的'b'以二進制模式打開文件:數據會以位元組對象的形式讀出和寫入。
IT培訓http://www.kmbdqn.cn/發現這種模式應該用於所有不包含文本的文件。
在文本模式下,讀取時默認會將平台有關的行結束符(Unix上是 ,Windows上是 )轉換為 。
在文本模式下寫入時,默認會將出現的 轉換成平台有關的行結束符。
這種暗地裡的修改對ASCII文本文件沒有問題,但會損壞JPEG或EXE這樣的二進制文件中的數據。
使用二進制模式讀寫此類文件時要特別小心。

7. python判斷字元串(string)是否包含(contains)子字元串的方法的代碼

下邊內容是關於python判斷字元串(string)是否包含(contains)子字元串的方法的內容。

方法2:使用find函數實現contains的功能

s = "This be a string"

if s.find("is") == -1:

    print "No 'is' here!"

else:

    print "Found 'is' in the string."

8. Python 基礎知識全篇-字元串(Strings)

單引號和雙引號

字元串可以包含在單引號或雙引號中。

這種靈活的方式可以讓我們在字元串中包含引號。

當我們需要創建一個多行字元串的時候,可以用三個引號。如下所示:

改變大小寫

你可以很方便的改變字元串的大小寫。如下所示:

最常見的大小寫形式是全小寫(lower),首字母大寫(title)和全大寫(upper)。如下所示:

注意:初始字元串沒被改變。

你會經常見到這種用法。變數名後跟點和操作名稱,且後跟一組圓括弧。圓括弧里可能是空的,也可能包含一些數據。

variable_name.action()

在這個例子中, action  是一個 方法 的名字。 title ,  lower ,  upper  是內置在 Python 中的函數,可以作用於字元串的方法。

連接字元串

字元串連接示例如下所示:

加號連接兩個字元串。你可以使用任意個加號來連接字元串。

格式化字元串簡介

空白符

空白符通常指計算機能夠發現但不可見的字元。諸如空格,製表符,換行符等。

空格很容易創建,基本上在你擁有計算機的時候就會打出空格符。製表符和換行符是由特殊字元連接組成的。

"\t" 代表製表符,"\n" 代表換行符。你可以將它們添加進字元串的任意部分。

去除空白符

有時候我們想去除掉字元串開始或者結尾的空白符。Python 中有一些方法可以幫我們做到這點。如下所示:

lstrip  去除左側開端的空白符, rstrip  去除右端結尾的空白符, strip  去除兩端空白符。

看一個更清晰的例子,如下所示:

動手試一試

Someone Said

找一條自己喜歡的名言,存儲在變數。結合適當的介紹列印出來。例如:" Ken Thompson once said, 'One of my most proctive days was throwing away 1000 lines of code' "。

First Name Cases

將你的姓存儲在一個變數中。

分別用 lowercase, Titlecase, UPPERCASE 三種方式列印姓。

Full Name

將你的名和姓存儲在不同的變數中,連接它們並列印。

Name Strip

將你的姓存儲在變數中。在姓的前後兩端至少各包含兩種空白符。

列印姓。

分別列印出去掉左側空白符,右側空白符,都去掉空白符的姓。

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:581
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:875
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:570
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:756
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:672
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:999
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:242
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:102
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:794
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:700