当前位置:首页 » 编程语言 » python里的string类型

python里的string类型

发布时间: 2022-04-26 11:42:50

A. python 改变string怎么改

  1. python中的string属于immutable数据结构,也就是说"不可修改",需要一个不同的string的时候,你只能创建一个新的string,比如str2 = str1.replace('a', 'b'),创建了一个字符串str2,内容是将str1中的a全部替换成b。

  2. 这样设计的目的主要有两点:

    1. 时间性能:不可变意味着字符串永远不需要拷贝内容,只要拷贝一份地址,即传递引用即可。

    2. 线程安全:不可变的特性天然保证了线程安全。

B. python中u'string'和unicode('string')有什么区别

首先要弄清楚的是,在python里,string object和unicode object是两种不同的类型。

string object是由characters组成的sequence,而unicode object是Unicode code units组成的sequence。

string里的character是有多种编码方式的,比如单字节的ASCII,双字节的GB2312等等,再比如UTF-8。很明显要想解读string,必需知道string里的character是用哪种编码方式,然后才能进行。

Unicode code unit又是什么东西呢?一个Unicode code unit是一个16-bit或者32-bit的数值,每个数值代表一个unicode符号。在python里,16-bit的unicode,对应的是ucs2编码。32-bit对应的是ucs4编码。是不是感觉string里character的编码没什么区别?反正我现在脑子里就是这样一个印象:在Python里,ucs2或者ucs4编码的,我们叫做unicode object,其他编码的我们就叫做string。

至于python里的unicode到底是ucs2还是ucs4的,可以在编译时指定。例如Linux下,要用ucs2做unicode的编码,可以这样
# ./configure --enable-unicode=ucs2
# make
# make install
下载的Windows预编译版本,一般都是ucs2的。要想知道某个python运行环境是ucs2还是ucs4,可以查看sys.maxunicde,65535就是ucs2的,另一个很大的数值就是ucs4。

下面我们看看string和unicode在python里的不同
我们先看看在简体中文Windows 2003系统下,系统编码是GBK
>>> a = '你好'
>>> a
'/xc4/xe3/xba/xc3'
>>> b = u'你好'
>>> b
u'/u4f60/u597d'
>>> print a
你好
>>> print b
你好
>>> a.__class__

>>> b.__class__

>>> len(a)
4
>>> len(b)
2

在一个系统编码为UTF-8的Linux环境下
>>> a = '你好'
>>> a
'/xe4/xbd/xa0/xe5/xa5/xbd'
>>> b = u'你好'
>>> b
u'/u4f60/u597d'
>>> print a
你好
>>> print b
你好
>>> a.__class__

>>> b.__class__

>>> len(a)
6
>>> len(b)
2

如何?简单总结一下:
1、string直接用引号来表示,unicode在引号前加一个u
2、直接输入的string常量会用系统缺省编码方式来编码,例如在GBK环境下,'你好'会编码成'/xc4/xe3/xba/xc3',而在UTF-8环境下就成了'/xe4/xbd/xa0/xe5/xa5/xbd'。
3、len(string)返回string的字节数,len(unicode)返回的是字符数
4、很重要的一点,print unicode不会乱码。现在我们常用的Linux、Windows系统,都是支持unicode的,版本太老的不算。比如Windows 2003支持ucs2,所以在中文Windows2003下,除了可以正常显示缺省的GBK编码外,还可以正常显示ucs2编码。举个例子,还是在中文Windows 2003的GBK环境下:
>>>a = '/xe4/xbd/xa0/xe5/xa5/xbd' # UTF-8的'你好'
>>> print a
浣犲ソ
>>> b = unicode(a, "UTF-8")
>>> b
u'/u4f60/u597d'
>>> print b
你好

应该明白了吧?

下面再说说string和unicode的相互转换,什么unicode()、decode()、encode()、codecs之类的。

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

D. python的string

意思是把奇数变成0吧?

判断每一位,如果是奇数就改为0

E. Python 中string问题

string是早期版本Python的模块,已经弃用。字符串应该使用内置类型str。不要定义和内置函数/对象名称相同的变量名,将代码中的str改成其他名字。

F. python的基本数据类型有哪些

python基本内置数据类型有哪些
一些基本数据类型,比如:整型(数字)、字符串、元组、列表、字典和布尔类型。
随着学习进度的加深,大家还会接触到更多更有趣的数据类型,python初学者入门时先了解这几种类型就可以了。
基本内置数据类型对应符号
1)整型——int——数字
python有5种数字类型,最常见的就是整型int。例如:1234、-1234
2)布尔型——bool——用符号==表示
布尔型是一种比较特殊的python数字类型,它只有True和False两种值,它主要用来比较和判断,所得结果叫做布尔值。例如:3==3
给出True,3==5给出False
3)字符串——str——用'
'或"
"表示
例如:'www.iplaypython.com'或者"hello"
4)列表——list——用[
]符号表示
例如:[1,2,3,4]
5)元组——tuple——用(
)符号表示
例如:('d',300)
6)字典——dict——用{
}符号表示
例如:{'name':'coco','country':'china'}

G. python基本数据类型有哪些

python基本数据类型有哪些?
python基本数据类型有:
● int 整型
● bool 布尔
● strintg 字符串
● pst 列表
● tuple 元组
● dict 字典
数据类型的可变和不可变
● 不可变类型:int, string,tuple
● 可变类型:pst,dict
相关推荐:《Python教程》以上就是小编分享的关于python基本数据类型有哪些的详细内容希望对大家有所帮助,更多有关python教程请关注环球青藤其它相关文章!

H. python中string和Unicode的区别

首先要弄清楚的是,在python里,string object和unicode object是两种不同的类型。

string object是由characters组成的sequence,而unicode object是Unicode code units组成的sequence。

string里的character是有多种编码方式的,比如单字节的ASCII,双字节的GB2312等等,再比如UTF-8。很明显要想解读string,必需知道string里的character是用哪种编码方式,然后才能进行。

Unicode code unit又是什么东西呢?一个Unicode code unit是一个16-bit或者32-bit的数值,每个数值代表一个unicode符号。在python里,16-bit的unicode,对应的是ucs2编码。32-bit对应的是ucs4编码。是不是感觉string里character的编码没什么区别?反正我现在脑子里就是这样一个印象:在Python里,ucs2或者ucs4编码的,我们叫做unicode object,其他编码的我们就叫做string。

至于python里的unicode到底是ucs2还是ucs4的,可以在编译时指定。例如Linux下,要用ucs2做unicode的编码,可以这样
# ./configure --enable-unicode=ucs2
# make
# make install
下载的Windows预编译版本,一般都是ucs2的。要想知道某个python运行环境是ucs2还是ucs4,可以查看sys.maxunicde,65535就是ucs2的,另一个很大的数值就是ucs4。

下面我们看看string和unicode在python里的不同
我们先看看在简体中文Windows 2003系统下,系统编码是GBK
>>> a = '你好'
>>> a
'/xc4/xe3/xba/xc3'
>>> b = u'你好'
>>> b
u'/u4f60/u597d'
>>> print a
你好
>>> print b
你好
>>> a.__class__
<type 'str'>
>>> b.__class__
<type 'unicode'>
>>> len(a)
4
>>> len(b)
2

在一个系统编码为UTF-8的Linux环境下
>>> a = '你好'
>>> a
'/xe4/xbd/xa0/xe5/xa5/xbd'
>>> b = u'你好'
>>> b
u'/u4f60/u597d'
>>> print a
你好
>>> print b
你好
>>> a.__class__
<type 'str'>
>>> b.__class__
<type 'unicode'>
>>> len(a)
6
>>> len(b)
2

如何?简单总结一下:
1、string直接用引号来表示,unicode在引号前加一个u
2、直接输入的string常量会用系统缺省编码方式来编码,例如在GBK环境下,'你好'会编码成'/xc4/xe3/xba/xc3',而在UTF-8环境下就成了'/xe4/xbd/xa0/xe5/xa5/xbd'。
3、len(string)返回string的字节数,len(unicode)返回的是字符数
4、很重要的一点,print unicode不会乱码。现在我们常用的Linux、Windows系统,都是支持unicode的,版本太老的不算。比如Windows 2003支持ucs2,所以在中文Windows2003下,除了可以正常显示缺省的GBK编码外,还可以正常显示ucs2编码。举个例子,还是在中文Windows 2003的GBK环境下:
>>>a = '/xe4/xbd/xa0/xe5/xa5/xbd' # UTF-8的'你好'
>>> print a
浣犲ソ
>>> b = unicode(a, "UTF-8")
>>> b
u'/u4f60/u597d'
>>> print b
你好

应该明白了吧?

下面再说说string和unicode的相互转换,什么unicode()、decode()、encode()、codecs之类的。

I. python中的元素加.string表示什么意思

应该是说元素类型。python是当下十分火爆的编程语言,尤其在人工智能应用方面。如果有心从事编程方向的工作,最好到专业机构深入学习、多实践,更贴近市场,这样更有利于将来的发展。

热点内容
汽车小组件怎么弄到安卓桌面 发布:2025-05-16 13:51:12 浏览:218
linuxg编译器下载 发布:2025-05-16 13:50:58 浏览:775
centosc编译器 发布:2025-05-16 13:50:17 浏览:947
安卓手机如何变换桌面 发布:2025-05-16 13:39:33 浏览:514
sql存储过程命令 发布:2025-05-16 13:17:54 浏览:145
用纸做解压小玩具西瓜 发布:2025-05-16 13:04:09 浏览:935
局域网xp无法访问win7 发布:2025-05-16 13:03:58 浏览:942
油卡如何修改密码 发布:2025-05-16 13:00:35 浏览:901
安卓手机如何拼照片 发布:2025-05-16 12:58:23 浏览:374
深入浅出python 发布:2025-05-16 12:56:52 浏览:655