当前位置:首页 » 编程语言 » python中string

python中string

发布时间: 2022-07-01 12:36:21

‘壹’ 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()

‘贰’ python中,import string是什么作用

import string就是引入string模块,使得我们可以调用与字符串操作相关的函数。
在Python中我们用import或者from ____ import ____来导入相应的模块。这类似于C语言中的include头文件。
模块其实就是一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序中,我们就可以使用了。

‘叁’ python中string(.)是什么意思

string(.)是语法错误。string. 表示调用标准库里面的string模块的方法。

‘肆’ python中string的操作里ljust是什么意思

这是填充字符用的。
用法:string.ljust(s,width[,fillchar])
意思就是如果你的字符串本来长度是5,我要把它变成长度为40,而且可以用字符填充。
>>> import string
>>> s="hello"
>>> string.ljust(s,40)
'hello '
>>> string.ljust(s,40,'x')
''
>>>

‘伍’ 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)python中string扩展阅读:

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和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和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之类的。

‘捌’ python的string

意思是把奇数变成0吧?

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

‘玖’ 如何使用python3中的字符串string基础

打开python3,这里小编使用的是python3.6,版本并不是太重要哈
建立变量str,为其赋值“abcdefg”,将其打印输出,如图所示

在打印输出的时候可以截取其中的部分文本,例如print(str[0:-1]),python中的文本索引是从0开始的,-1则是末尾的开始位置,可以输入print(str[-1])查看最后一个文字

如果需要重复输出文本可以使用print(str*数字)这种格式,数字表示文本重复的次数 ,比如print(str*2)

“+”既是运算符号,也是文本连接符号,连接字符串的时候使用就好了
如print(str+“ma ma hong”)

转移字符使用"\",我们重用的换行就是“\n”
print("my\n name")
如果不需要转义,就在字符串前面加“r”即 print(r"my\n name")
结果如图

需要注意的是python里面的字符串不能更改,如将str的首字母改为L
str[0]=L python会报错

7
此外,还有就是格式化字符串,和C是一致的,搞不清是什么类型的时候用%s就好了
如“python %s”% “language”

热点内容
网页设置的密码如何删除 发布:2024-05-06 02:20:30 浏览:925
如何查看snmp配置信息 发布:2024-05-06 02:19:48 浏览:487
预科编程 发布:2024-05-06 02:19:42 浏览:138
压缩比英文 发布:2024-05-06 01:56:35 浏览:171
数字php 发布:2024-05-06 01:53:10 浏览:742
编程中怎么 发布:2024-05-06 01:43:32 浏览:629
如何访问远程数据库 发布:2024-05-06 01:39:20 浏览:447
刷算法的网站 发布:2024-05-06 01:30:39 浏览:270
少儿编程徐州 发布:2024-05-06 01:20:42 浏览:462
sqlserver连接驱动 发布:2024-05-06 00:33:34 浏览:646