【Python基础学习篇九】Python字符串

一、字符串的操作

字符串是Python的一种基本类型,字符串的操作包括字符串的格式化、字符串的截取、过滤、合并、查找等操作。

二、字符串的格式化

Python将若干值插入到带有“%”标记的字符串中,从而可以动态的输出字符串。

字符串的格式化语法如下:

"% s" % str1
"%s %s" % (str1,str2)

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#格式化字符串
str1 = ‘version‘
num = 1.0
format = "% s" % str1
print format
format = "% s % d" % (str1,num)
print format

输出结果:

---------- python2.7 ----------
version
version  1

输出完成 (耗时 0 秒) - 正常终止

说明:

1)%d表示替换的值为整型。

2)如果要格式化多个值,元组中元素的顺序必须和格式化字符串中替代符的顺序一致,否则可能出现类型不匹配的问题。

例如:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#格式化字符串
str1 = ‘version‘
num = 1.0
format = "% s" % str1
print format
format = "% d % s" % (str1,num)
print format

输出结果:

---------- python2.7 ----------
version
Traceback (most recent call last):
  File "Noname1.py", line 10, in <module>
    format = "% d % s" % (str1,num)
TypeError: %d format: a number is required, not str

输出完成 (耗时 0 秒) - 正常终止

使用%f可以格式化浮点数的精度,根据指定的精度做“四舍五入”。

例如:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#带精度的格式化
print "浮点型数字:%f" % 1.25
print "浮点型数字:%.1f" % 1.26
print "浮点型数字:%.2f" % 1.254

输出结果:

---------- python2.7 ----------
浮点型数字:1.250000
浮点型数字:1.3
浮点型数字:1.25

输出完成 (耗时 0 秒) - 正常终止

Python格式化字符串的替代符及其含义

注意:

如果要在字符串中输出“%”,则需要使用“%%”。

用字典格式化多个值,并指定格式化字符串的名称。

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#使用字典格式化字符串
print "%(version)s:%(num) .1f" % {"version":"version","num":2}

输出结果:

---------- python2.7 ----------
version: 2.0

输出完成 (耗时 0 秒) - 正常终止

Python可以实现字符串的对齐操作,还提供了用于字符串对齐的函数。

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#字符串对齐
word = "version3.0"
print word.center(20)
print word.center(20,"*")
print word.ljust(0)
print word.rjust(20)
print "%30s" % word

输出结果:

---------- python2.7 ----------
     version3.0
*****version3.0*****
version3.0
          version3.0
                    version3.0

输出完成 (耗时 0 秒) - 正常终止

说明:

1)ljust()输出结果左对齐;

2)rjust()输出结果右对齐。参数20表示一共输出20个字符,version3.0占用10个字符,左边填充10个空格;

3)%30s表示先输出20个空格,类似word.rjust(30)

三、字符串的转义符

Python使用“\”作为转义字符。

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#输出转义字符
path = "hello\tworld\n"
print path
print len(path)
path = r"hello\tworld\n"
print path
print len(path)

输出结果:

---------- python2.7 ----------
hello    world

12
hello\tworld\n
14

输出完成 (耗时 0 秒) - 正常终止

注意:

Python的制表符只占1个字符的位置,而不是2个或4个字符的位置。

Python的转移字符及其含义

注意:

如果要在字符串中输出“\”,需要使用“\\”

Python还提供了函数strip()、lstrip()、rstrip()去掉字符串中的转义符:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#strip()去掉转义字符
word = "\thello world\n"
print "直接输出:",word
print "strip()后输出:",word.strip()
print "lstrip()后输出:",word.lstrip()
print "rstrip()后输出:",word.rstrip()

输出结果:

---------- python2.7 ----------
直接输出:     hello world

strip()后输出: hello world
lstrip()后输出: hello world

rstrip()后输出:     hello world

输出完成 (耗时 0 秒) - 正常终止

四、字符串的合并

Python使用“+”连接不同的字符串。Python会根据“+”两侧变量的类型,决定指定连接操作或加法运算。如果“+”两侧都是字符串类型,则进行连接操作;如果“+”两侧都是数字类型,则进行加法运算;如果“+”两侧是不同的类型,将抛出异常。

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#使用“+”连接字符串
str1 = "hello"
str2 = "world"
str3 = "hello"
str4 = "China"
result = str1 + str2 + str3
result += str4
print result

输出结果:

---------- python2.7 ----------
helloworldhelloChina

输出完成 (耗时 0 秒) - 正常终止

Python还提供了函数join()连接字符串,join()配合列表实现多个字符串的连接。

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#使用join()连接字符串
strs = ["hello","world","hello","China"]
result = "".join(strs)
print result

输出结果:

---------- python2.7 ----------
helloworldhelloChina

输出完成 (耗时 0 秒) - 正常终止

还可以使用reduce()函数,reduce()的作用就是对某个变量进行累计。这里可以对字符串进行累计连接,从而实现多个字符串进行连接的功能。

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#使用reduce()连接字符串
import operator
strs = ["hello","world","hello","China"]
result = reduce(operator.add,strs,"")
print result

输出结果:

---------- python2.7 ----------
helloworldhelloChina

输出完成 (耗时 0 秒) - 正常终止

五、字符串的截取

字符串的截取是实际应用中经常使用的技术,被截取的部分称之为“子串”。Python可以通过“索引”、“切片”获取子串,也可以使用函数split()来获取。字符串属于序列。

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#使用索引截取子串
word = "world"
print word[4]

输出结果:

---------- python2.7 ----------
d

输出完成 (耗时 0 秒) - 正常终止

通过切片可以实现对字符串有规律的截取。切片的语法格式如下:

string[start:end:step]

说明:

string表示取子串的原字符串变量;

[start:end:step]表示从string的第start个索引位置开始到第end个索引之间截取子串,截取的步长是step。即每次截取字符string[start + step],直到第end个索引。索引从0开始计数。

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#特殊切片截取子串
word = "world"
str1 = "hello world"
print word[0:3]
print str1[::2]
print str1[1::2]

输出结果:

---------- python2.7 ----------
wor
hlowrd
el ol

输出完成 (耗时 0 秒) - 正常终止

如果要同时截取多个子串,可以使用函数split()实现。函数split()的声明如下表示:

split([char][,num])

说明:

1)参数char表示用于分割的字符,默认的分割字符是空格;

2)参数num表示分割的次数。如果num等于2,将把源字符分割为3个子串。默认情况下,将根据字符char在字符串中国出现的个数来分割子串。

3)函数的返回值是由子串组成的列表。

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#使用split()获取子串
sentence = "Bob said:1,2,3,4"
print "使用空格取子串:",sentence.split()
print "使用逗号取子串:",sentence.split(",")
print "使用两个逗号取子串:",sentence.split(",",2)

输出结果:

---------- python2.7 ----------
使用空格取子串: [‘Bob‘, ‘said:1,2,3,4‘]
使用逗号取子串: [‘Bob said:1‘, ‘2‘, ‘3‘, ‘4‘]
使用两个逗号取子串: [‘Bob said:1‘, ‘2‘, ‘3,4‘]

输出完成 (耗时 0 秒) - 正常终止

字符串连接后,Python将分配新的空间给连接后的字符串,源字符串保持不变:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
str1 = "a"
print id(str1)
print id(str1 + "b")

输出结果:

---------- python2.7 ----------
34320512
34818128

输出完成 (耗时 0 秒) - 正常终止

六、字符串的比较

Python直接使用“==”、“!=”运算符比较两个字符串的内容。如果比较的两个变量的类型不相同,比较的内容也不相同。

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#字符串的比较
str1 = 1
str2 = "1"
if str1 == str2:
    print "相同"
else:
    print "不相同"
if str(str1) == str2:
    print "相同"
else:
    print "不相同"

输出结果:

---------- python2.7 ----------
不相同
相同

输出完成 (耗时 0 秒) - 正常终止

如果要比较字符串中的一部分内容,可以先截取子串,再使用“==”运算符进行比较。如果要比较字符串的开头或者结尾部分,更方便的方法是使用startswith()或endswith()函数。

startswith()的声明如下:

startswith(substring,[,start[,end]])

说明:

1)参数substring是与源字符串开头部分比较的子串;

2)参数start表示开始比较的位置;

3)参数end表示比较结束的位置,即在“start:end”范围内搜索子串substring。

endswith()的参数和返回值类似startswith(),不同的是endswith()从源字符串的尾部开始搜索。

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#比较字符串的开始和结束处
word = "hello world"
print "hello" == word[0:5]
print word.startswith("hello")
print word.endswith("ld",6)
print word.endswith("ld",6,10)
print word.endswith("ld",6,len(word))

输出结果:

---------- python2.7 ----------
True
True
True
False
True

输出完成 (耗时 0 秒) - 正常终止

说明:

startswith()、endswith()相当于分片[0:n],n是源字符串中最后一个索引。startswith()、endswith()不能用于比较源字符串中任意的一部分的子串。

七、字符串的反转

字符串反转是指字符串中最后一个字符移到字符串的第一个位置,按照倒序的方式依次前移。

Python中没有提供对字符串进行反转的函数,但是可以使用列表和字符串索引来实现字符串的反转,并通过range()进行循环。

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#循环输出反转的字符串
def reverse(s):
    out = ""
    li = list(s)
    for i in range(len(li),0,-1):
        out += "".join(li[i-1])
    return out

函数reverse()的调用:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#循环输出反转的字符串
def reverse(s):
    out = ""
    li = list(s)
    for i in range(len(li),0,-1):
        out += "".join(li[i-1])
    return out

print reverse("hello")

输出结果:

---------- python2.7 ----------
olleh

输出完成 (耗时 0 秒) - 正常终止

还可以通过列表的reverse()函数实现字符串的反转,这样代码将大大简化

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#使用list的reverse()
def reverse(s):
    li = list(s)
    li.reverse()
    s = "".join(li)
    return s

Python的列表是对字符串进行处理的常用方式,灵活使用列表等内置数据结构处理字符串,能够降低编程的复杂度。利用序列的“切片”实现字符串的反转最为简洁,如下:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
def reverse(s):
    return s[::-1]

-1表示从字符串最后一个索引开始排序排列。

八、字符串的查找和替换

1、查找字符串

Python提供函数find(),返回源字符串中第1次出现子串的索引,rfind()表示从右往左查找。

find()的声明如下表示:

find(substring[,start[,end]])

说明:

参数substring表示待查找的子串;

参数start表示开始搜索的索引位置;

参数end表示结束搜索的索引位置,即在分片[start:end]中进行查找;

如果找到字符串substring,则返回substring在源字符串中第1次出现的索引,否则返回-1.

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#查找字符串
sentence = "This is a apple"
print sentence.find("a")
sentence = "This is a apple"
print sentence.rfind("a")

输出结果:

---------- python2.7 ----------
8
10

输出完成 (耗时 0 秒) - 正常终止

2、替换字符串

Python使用函数replace()实现字符串的替换,该函数可以指定替换的次数。replace()不支持正则表达式的语法。

replace()的声明如下:

replace(old,new[,max])

说明:

参数old表示将被替换的字符串;

参数new表示替换old的字符串;

参数max表示使用new替换old的次数;

函数返回一个新的字符串.如果子串old不在源字符串中,则函数返回源字符串的值.

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
#字符串的替换
centence = "hello world,hello china"
print centence.replace("hello","hi")
print centence.replace("hello","hi",1)
print centence.replace("abc","hi")

输出结果:

---------- python2.7 ----------
hi world,hi china
hi world,hello china
hello world,hello china

输出完成 (耗时 0 秒) - 正常终止

说明:

最后一行代码,由于centence没有子串”abc”,所以替换失败。replace()返回centence的值。

九、字符串与日期的转换

1、从时间到字符串的转换

times模块中的函数strftime()可以实现从时间到字符串的转换。strftime()的声明如下所示:

strftime{format[,tuple]} -> string

说明:

参数format表示格式化日期的特俗字符.例如”%Y-%m-%d”相当于“yyyy-MM-dd”;

参数tuple表示需要转换的时间,用元组存储。元组中的元素分别表示年、月、日、时、分、秒。

函数返回一个表示时间的字符串。

参数format格式化日期的常用标记

2、字符串到时间的转换

字符串到时间的转换需要进行两次转换,需要使用time模块和datetime类,转换过程分为如下3个步骤:

(1)调用函数strptime()把字符串转换为一个元组,进行第1次转换。

strptime()的声明如下:

strptime(string,format) -> struct_time

参数string表示需要转换的字符串;

参数format表示日期时间的输出格式;

函数返回一个存放时间的元组.

(2)把表示时间的元组赋值给表示年、月、日的3个变量。

(3)把表示年、月、日的3个变量传递给函数datetime(),进行第2次转换。datetime类的datetime()函数如下所示:

datetime(year,month,day[,hour[,minute[,second[,microsecond[,tzinfo]]]]])

说明:

参数year、month、day分别表示年、月、日,这3个参数必不可少;

函数返回1个datetime类型的变量。

例子:

#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.7
#Author: [email protected]
import time,datetime

#时间到字符串的转换
print time.strftime("%Y-%m-%d%x",time.localtime())
#字符串到时间的转换
t = time.strptime("2014-08-08","%Y-%m-%d")
y,m,d = t[0:3]
print datetime.datetime(y,m,d)

输出结果:

---------- python2.7 ----------
2014-12-0312/03/14
2014-08-08 00:00:00

输出完成 (耗时 0 秒) - 正常终止

说明:

函数localtime()返回当前的时间,strftime把当前的时间格式化为字符串类型;

格式化日期的特殊标记是区分大小写的,%Y和%y并不相同。

时间: 2024-08-02 07:02:07

【Python基础学习篇九】Python字符串的相关文章

【Python基础学习篇】Python序列

序列是具有索引和切片功能的集合.元组.列表和字符串具有通过索引访问某个具体的值,或通过切片返回一段切片的能力,因此元组.列表和字符串都属于序列. 例子:序列的索引功能 #!/usr/bin/env python # -*- coding=utf-8 -*- #Using GPL v3.3 #Author: [email protected] #索引操作 tuple = ("apple","banana","grape","orange&

【Python基础学习篇】Python元组

元组是Python中内置的一种数据结构.元组由不同的元素组成,每个元素可以存储不同类型的数据,如字符串.数字甚至元组.元组是写保护的,即元组创建后不能再做任何修改操作,元组通常代表一行数据,而元组中的元素代表不同的数据项. 一.元组的创建 Tuple(元组)由一系列元素组成,所有元素被包含在一对圆括号中.创建元组时,可以不指定元素的个数,相当于不定长的数组,但是一旦创建后就不能修改元组的长度. 元组创建的格式如下所示: tuple_name = (元素1,元素2,...) 例子:元组的初始化 t

【Python基础学习篇】Python控制语句

一.结构化程序设计 结构化程序设计是以模块化设计为中心,将待开发的软件系统划分为若干个相互独立的模块. 下图为描述判断摸个数字是属于正数.负数还是零的流程图. 结构化程序设计的主要方法是-自顶向下.逐步细化.把需要解决的问题分成若干个任务来完成,再对每个任务进行设计,逐步细化. 结构化程序设计分为3中结构: 1.顺序结构 2.判断结构 3.循环结构 --------------------------------------------------------------------------

【Python基础学习篇】Python字典

字典(Dictionary)是由"键-值"对组成的集合,字典中的"值"通过"键"来引用. 一.字典的创建 字典由一系列的"键-值"(key-value)对组成,"键-值"对之间用"逗号"隔开,并且被包含在一对花括号中. 创建字典的格式如下: dictionary_name = {key1:value1,key2:value2,...} 如果需要创建一个空的字典,只需要一对花括号即可,如下

【Python基础学习篇】Python基本语法

一.Python的文件类型 Python的文件类型主要分为3种,分别是源代码.字节代码和优化代码. 1.源代码 以py为扩展名 2.字节代码 Python源文件经过编译后生成扩展名为pyc的文件,pyc是编译过的字节文件.这种文件不能使用文本编辑工具打开或修改. 下面这段脚本可以把hello.py文件编译为hello.pyc文件 import py_compile py_compile compile("hello.py") 将此脚本保存为以py为扩展名的文件,运行后即可得到hello

【Python基础学习篇】Python列表

列表(List)通常作为函数的返回类型.列表和元组相似,也是由一组元素组成,列表可以实现添加.删除和查找操作,元素的值可以被修改. 一.列表的创建 列表是由一系列元素组成,所有的元素都被包含在一对方括号中.列表创建后,可以执行添加.删除或修改操作. 列表的创建格式如下所示: list_name = [元素1,元素2,...] 列表的添加可以调用append(),该方法的声明如下所示: append(object) 其中,object可以是元组.列表.字典或任何对象.append()每次添加的新元

Python基础学习(九)

Python 多线程 多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进度条来显示处理的进度 程序的运行速度可能加快 在一些等待的任务实现上如用户输入.文件读写和网络收发数据等,线程就比较有用了.在这种情况下我们可以释放一些珍贵的资源如内存占用等等. 线程在执行过程中与进程还是有区别的.每个独立的线程有一个程序运行的入口.顺序执行序列和程序的出口.

【Python基础学习篇十】Python正则表达式(2015-01-01)

一.正则表达式简介 正则表达式用于文本匹配的工具,在源字符串中查找与给定的正则表达式相匹配的部分.一个正则表达式是由字母.数字和特殊字符组成的.正则表达式中有许多特殊的字符,这些特殊字符是构成正则表达式的要素. 1.正则表达式中的特殊字符: 2.正则表达式中的常用限定符: 利用{}可以控制字符重复的次数. 例如,\d{1,4}表示1位到3位的数字: 某些地区的电话号码是8位数,区号也有可能是3位或4位数字. \d{3}-\d{8}|\d{4}-\d{7} 3.限定符与"?"的组合 二.

【Python基础学习篇八】Python函数

函数是一段可以重复多次调用的代码,通过输入的参数值,返回需要的结果. 一.函数的定义 函数的定义使用保留字def定义.函数在使用前必须定义,函数的类型即返回值的类型. Python函数定义的格式如下: def 函数名 (参数1[=默认值1],参数2[=默认值2]...): ... return 表达式 函数名可以是字母.数字或下划线组成的字符串,但不能以数字开头.函数的参数放在一对圆括号中,参数的个数可以有一个或多个,参数之间用逗号隔开,这种参数称之为形式参数. 例子: #!/usr/bin/e