当前位置:首页 » 编程语言 » stringpython

stringpython

发布时间: 2022-12-30 14:10:26

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、系统路径

这样就不用专门的去处理引号之中的特殊字符了

热点内容
安卓西瓜视频如何去水印 发布:2025-07-02 11:58:40 浏览:961
算法所需 发布:2025-07-02 11:50:29 浏览:310
linux删除组中的用户 发布:2025-07-02 11:48:09 浏览:367
编程员是什么 发布:2025-07-02 11:43:03 浏览:636
如何更改笔记本电脑密码方式 发布:2025-07-02 11:34:07 浏览:52
安卓平板不知道怎么选 发布:2025-07-02 11:33:22 浏览:16
qq空间怎么设密码 发布:2025-07-02 11:30:29 浏览:719
跑跑卡丁车如何更改服务器 发布:2025-07-02 11:27:35 浏览:300
我的世界112服务器存档 发布:2025-07-02 11:25:02 浏览:948
php类函数调用 发布:2025-07-02 11:24:27 浏览:26