当前位置:首页 » 编程语言 » 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

将你的姓存储在变量中。在姓的前后两端至少各包含两种空白符。

打印姓。

分别打印出去掉左侧空白符,右侧空白符,都去掉空白符的姓。

热点内容
手机存储与内部存储空间 发布:2024-04-30 10:05:14 浏览:41
博图v15触摸屏编译完后无法仿真 发布:2024-04-30 10:00:14 浏览:183
安卓屏怎么设置原车logo 发布:2024-04-30 09:23:06 浏览:773
我的世界手机版20多万赞的服务器 发布:2024-04-30 09:18:57 浏览:864
笔记本cpu配置参数怎么看 发布:2024-04-30 09:14:56 浏览:544
力软敏捷开发框架源码 发布:2024-04-30 08:33:57 浏览:168
我的世界网易最古老服务器 发布:2024-04-30 08:33:06 浏览:38
缓存合并转码 发布:2024-04-30 08:31:02 浏览:170
苏州哪里学java 发布:2024-04-30 08:29:34 浏览:807
java导入源码 发布:2024-04-30 07:58:41 浏览:307