Python序列——字符串
本文介绍Python序列中的字符串。
1. 字符串
字符串支持序列操作。
1.1 string模块预定义字符串
>>> import string
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'
>>>
1.2 普通字符串与Unicode字符串
>>> u'Hello' + ' furzoom'
u'Hello furzoom'
>>>
1.3 只适用于字符串的操作
字符串格式化符号
格式化字符 | 转换方式 |
---|---|
%c | 转换成字符(ASCII码值,或者长度为一的字符串) |
%r | 优先用repr()函数进行字符串转换 |
%s | 优先用str()函数进行字符串转换 |
%d/%i | 转换成有符号十进制数 |
%u | 转换成无符号十进制数 |
%o | 转换成无符号八进制数 |
%x/%X | 转换成无符号十六进制数 |
%e/%E | 转换成科学计数法 |
%f/%F | 转换成浮点数 |
%g/%G | %e和%f/%E和%F的简写 |
%% | 转出% |
格式化操作辅助指令
符号 | 作用 |
---|---|
* | 定义宽度或者小数点精度 |
- | 左对齐 |
+ | 在正数前显示加号(+) |
<sp> | 在正数前显示空格 |
0 | 显示数字前填充0,而不是空格 |
# | 在八进制数前显示0,在十六进制前显示0x或者0X |
(var) | 映射变量(字典参数) |
m.n | m表示显示的最小总宽度,n是小数点后的位数 |
>>> '%x' % 108
'6c'
>>> '%X' % 108
'6C'
>>> '%#X' % 108
'0X6C'
>>> '%#x' % 108
'0x6c'
>>> '%f' % 1234.567890
'1234.567890'
>>> '%.2f' % 1234.567890
'1234.57'
>>> '%E' % 1234.567890
'1.234568E+03'
>>> '%e' % 1234.567890
'1.234568e+03'
>>> '%g' % 1234.567890
'1234.57'
>>> '%G' % 1234.567890
'1234.57'
>>> '%e' % 111111111111111111111
'1.111111e+20'
>>> 'Welcome to %(website)s, %(name)s' % {'name': 'mn', 'website': 'furzoom.com'}
'Welcome to furzoom.com, mn'
>>> from string import Template
>>> s = Template('There are ${howmany} ${lang} Quotation Symbols')
>>> print s.substitute(lang='Python', howmany=3)
There are 3 Python Quotation Symbols
>>>
>>> print s.substitute(lang='Python')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/string.py", line 172, in substitute
return self.pattern.sub(convert, self.template)
File "/usr/lib/python2.7/string.py", line 162, in convert
val = mapping[named]
KeyError: 'howmany'
>>>
>>> print s.safe_substitute(lang='Python')
There are ${howmany} Python Quotation Symbols
>>>
1.4 原始字符串
>>> '\n'
'\n'
>>> print '\n'
>>> r'\n'
'\\n'
>>> print r'\n'
\n
>>>
1.5 Unicode字符串操作符
他用Unicode原始字符串时,u
要出现在r
前面。
>>> ur'hello\nfurzoom'
u'hello\\nfurzoom'
>>> ru'hello\nmn'
File "<stdin>", line 1
ru'hello\nmn'
^
SyntaxError: invalid syntax
>>>
2. 内建函数
2.1 标准类型函数与序列操作函数
- cmp()
- len()
- max()
- min()
- enumerate()
- zip()
>>> s1 = 'furzoom'
>>> s2 = 'abcdefg'
>>> cmp(s1, s2)
1
>>> cmp(s2, s1)
-1
>>> cmp(s1, 'furzoom')
0
>>> len(s1)
7
>>> max(s1)
'z'
>>> min(s1)
'f'
>>> us1 = u'furzoom'
>>> len(us1)
7
>>> us1
u'furzoom'
>>> print us1
furzoom
>>> min(us1)
u'f'
>>> max(us1)
u'z'
>>> for i, t in enumerate(s1):
... print i, t
...
0 f
1 u
2 r
3 z
4 o
5 o
6 m
>>> zip(s2, s1)
[('a', 'f'), ('b', 'u'), ('c', 'r'), ('d', 'z'), ('e', 'o'), ('f', 'o'), ('g', 'm')]
>>>
2.2 字符串类型函数
- raw_input()
- str()
- unicode()
- chr()
- unichr()
- ord()
unichr()
如果配置为USC2的Unicode,参数范围是range(65535)
,如果配置为USC4的Unicode,那么参数范围是range(0x1100000)
。
>>> name = raw_input("Enter your name: ")
Enter your name: furzoom MN
>>> name
'furzoom MN'
>>> len(name)
10
>>> unicode(name)
u'furzoom MN'
>>> str(unicode(name))
'furzoom MN'
>>>
>>> isinstance(u'\0xAB', str)
False
>>> isinstance('mn', unicode)
False
>>> isinstance(u'', unicode)
True
>>> isinstance('mn', str)
True
>>> chr(65)
'A'
>>> ord('a')
97
>>> unichr(12345)
u'\u3039'
>>> chr(12345)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: chr() arg not in range(256)
>>> ord(u'\uffff')
65535
>>>
3. 字符串内建函数
- string.capitalize()
- string.center(width[, fillchar])
- string.count(sub[, start[, end]]])
- string.decode([encode[, errors]])
- string.encode([encode[, errors]])
- string.endswith(suffix[, start[, end]]])
- string.expandtabs([tabsize])
- string.find(sub[, start[, end]])
- string.format(*args, **kwargs)
- string.index(sub[, start[, end]])
- string.isalnum()
- string.isalpha()
- string.isdigit()
- string.islower()
- string.isspace()
- string.istitle()
- string.isupper()
- string.istitle()
- string.join(iterable)
- string.ljust(width[, fillchar])
- string.lower()
- string.lstrip([chars])
- string.partition(sep)
- string.replace(old, new[, count])
- string.rfind(sub[, start[, end]])
- string.rindex(sub[, start[, end]])
- string.rjust(width[, fillchar])
- string.rpartition(sep)
- string.rsplit([sep[, maxsplit]])
- string.rstrip([chars])
- string.split([sep[, maxsplit]])
- string.splitlines([keepends])
- string.startswith(prefix[, start[, end]])
- string.strip([chars])
- string.swapcase()
- string.title()
- string.translate(talbe[, deletechars])
- string.upper()
- string.zfill(width)
string.format()
将在后面进行介绍。
>>> s = 'welcome to visit furzoom.com'
>>> s.capitalize()
'Welcome to visit furzoom.com'
>>> s.center(50)
' welcome to visit furzoom.com '
>>> s.center(50, '#')
'###########welcome to visit furzoom.com###########'
>>> s.count('om')
3
>>> s.count('om', -10)
2
>>> s.count('om', 0, 10)
1
>>> s.decode()
u'welcome to visit furzoom.com'
>>> s.decode().encode()
'welcome to visit furzoom.com'
>>> s.endswith('com')
True
>>> s.endswith('')
True
>>> s.endswith('mn')
False
>>> s.endswith('co', 0, -1)
True
>>> s1 = '1\t23\t456\t789'
>>> s1.expandtabs()
'1 23 456 789'
>>> s1.expandtabs(4)
'1 23 456 789'
>>> s1.expandtabs(3)
'1 23 456 789'
>>> s1.expandtabs(5)
'1 23 456 789'
>>> s1.expandtabs(6)
'1 23 456 789'
>>> s.find('om')
4
>>> s.find('mn')
-1
>>> s.find('om', 5)
22
>>> s.index('om')
4
>>> s.index('mn')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> '234'.isalnum()
True
>>> s.isalnum()
False
>>> ''.isalnum()
False
>>> s.isalpha()
False
>>> 'furzoom'.isalpha()
True
>>> s.isdigit()
False
>>> '234'.isdigit()
True
>>> ''.isdigit()
False
>>> s.islower()
True
>>> '234'.islower()
False
>>> s.isspace()
False
>>> ' \t'.isspace()
True
>>> s.istitle()
False
>>> 'Welcome To Furzoom'.istitle()
True
>>> s.isupper()
False
>>> 'MN'.isupper()
True
>>> '#'.join([str(i) for i in range(10)])
'0#1#2#3#4#5#6#7#8#9'
>>> s.ljust(40)
'welcome to visit furzoom.com '
>>> s.ljust(40, '#')
'welcome to visit furzoom.com############'
>>> s.lower()
'welcome to visit furzoom.com'
>>> ss = s.center(40)
>>> ss
' welcome to visit furzoom.com '
>>> ss.lstrip()
'welcome to visit furzoom.com '
>>> ss.lstrip(' we')
'lcome to visit furzoom.com '
>>> s.partition('om')
('welc', 'om', 'e to visit furzoom.com')
>>> s.partition('mn')
('welcome to visit furzoom.com', '', '')
>>> s.replace('o', '#')
'welc#me t# visit furz##m.c#m'
>>> s.replace('o', '#', 3)
'welc#me t# visit furz#om.com'
>>> s.rfind('o')
26
>>> s.rfind('o', 25)
26
>>> s.rfind('o', -3)
26
>>> s.rfind('o', -3, -20)
-1
>>> s.rfind('o', 5, 15)
9
>>> s.rindex('om')
26
>>> s.rjust(40)
' welcome to visit furzoom.com'
>>> s.rjust(40, '#')
'############welcome to visit furzoom.com'
>>> s.rpartition('oom')
('welcome to visit furz', 'oom', '.com')
>>> s.rsplit()
['welcome', 'to', 'visit', 'furzoom.com']
>>> s.rsplit(' ', 2)
['welcome to', 'visit', 'furzoom.com']
>>> ss.rstrip()
' welcome to visit furzoom.com'
>>> ss.rstrip(' m')
' welcome to visit furzoom.co'
>>> 'ab\n\nde fg\rhi\r\n'.splitlines()
['ab', '', 'de fg', 'hi']
>>> 'ab\n\nde fg\rhi\r\n'.splitlines(True)
['ab\n', '\n', 'de fg\r', 'hi\r\n']
>>> ''.splitlines()
[]
>>> ''.split('\n')
['']
>>> 'line\n'.split('\n')
['line', '']
>>> 'line\n'.splitlines()
['line']
>>> s.startswith('wel')
True
>>> s.startswith(' ')
False
>>> ss.strip()
'welcome to visit furzoom.com'
>>> ss.strip(' wm')
'elcome to visit furzoom.co'
>>> s.swapcase()
'WELCOME TO VISIT FURZOOM.COM'
>>> s.title()
'Welcome To Visit Furzoom.Com'
>>> s.title().swapcase()
'wELCOME tO vISIT fURZOOM.cOM'
>>> s.translate(None, 'aeiou')
'wlcm t vst frzm.cm'
>>> import string
>>> s.translate(string.maketrans('aeiou', '12345'))
'w2lc4m2 t4 v3s3t f5rz44m.c4m'
>>> s.upper()
'WELCOME TO VISIT FURZOOM.COM'
>>> s.zfill(40)
'000000000000welcome to visit furzoom.com'
4. 字符串特有性质
4.1 转义字符
转义字符 | 十六进制 |
---|---|
\0 | 0x00 |
\a | 0x07 |
\b | 0x08 |
\t | 0x09 |
\n | 0x0A |
\v | 0x0B |
\f | 0x0C |
\r | 0x0D |
\e | 0x1B |
\” | 0x22 |
\’ | 0x27 |
\\ | 0x5C |
>>> print 'aaa\b\bbb'
abb
>>> print 'aaaaaaa\rbbc'
bbcaaaa
4.2 三引号
使用三引号,字符串可以包含换行符、制表符等其他特殊字符。常常在需要包含HTML和SQL语句时使用。
4.3 字符串是不可变数据类型
当修改一个字符串时,都是新建了一个字符串。
Python序列——字符串的更多相关文章
- Python——序列
#!/usr/bin/python #coding:utf8 ''' Python——序列 字符串的操作 ''' s = 'abcdefg' print s print s[2] print s[-1 ...
- 【循序渐进学Python】3. Python中的序列——字符串
字符串是零个或多个的字符所组成的序列,字符串是Python内建的6种序列之一,在Python中字符串是不可变的. 1. 格式化字符串 字符串格式化使用字符串格式化操作符即百分号%来实现.在%左侧放置一 ...
- python 序列:字符串、列表、元组
python 序列:字符串.列表.元组 序列:包含一定顺序排列的对象的一个结构 内建函数:str() list() tuple() 可以使用str(obj)可以把对象obj转换成字符串 list( ...
- Python 序列操作符与函数(字符串)
Python序列包括:元组.列表.字符串. 1.1 序列共同支持的函数: 函数 功能 说明 cmp(seq1,seq2) 比较序列大小 从左到右依次比较,直到比较出大小 len(seq1) 获取序列长 ...
- 『无为则无心』Python基础 — 16、Python序列之字符串的下标和切片
目录 1.序列的概念 2.字符串的下标说明 3.字符串的切片说明 1.序列的概念 序列sequence是Python中最基本的数据结构.指的是一块可存放多个值的连续内存空间,这些值按一定顺序排列,可通 ...
- 大爽Python入门教程 2-2 序列: 字符串、元组与列表
大爽Python入门公开课教案 点击查看教程总目录 序列 序列(sequence): 顾名思义,有序的排列. 有序排列的一串数据. 一种容器,容器内成员有序排列. python的字符串str,元组tu ...
- Python格式化字符串~转
Python格式化字符串 在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作 ...
- Python格式化字符串
在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作符,非常类似C语言里的pr ...
- python反转字符串(简单方法)及简单的文件操作示例
Python反转字符串的最简单方法是用切片: >>> a=' >>> print a[::-1] 654321 切片介绍:切片操作符中的第一个数(冒号之前)表示切片 ...
随机推荐
- 计蒜客 UCloud 的安全秘钥(随机化+Hash)
题目链接 UCloud 的安全秘钥 对于简单的版本,我们直接枚举每个子序列,然后sort一下判断是否完全一样即可. #include <bits/stdc++.h> using names ...
- Code Sign error: a valid provisioning profile matching the application's Identifier 'com. sensoSource.VoiceRecorder' could not be found
如果不是com. sensoSource.VoiceRecorder,在xxx-info.plist里Bundle identifier里替换成你的证书名 xxx是你的工程名 在Bundle iden ...
- 【java】Java transient关键字使用小记【转】
转载地址:https://www.cnblogs.com/lanxuezaipiao/p/3369962.html 1. transient的作用及使用方法 我们都知道一个对象只要实现了Seriliz ...
- 【spring boot】在Spring mvc中controller中可以拿到对象信息,但是返回给前台却是什么也没有,解决方案
如图所示: 最后: 问题解决: 这个原因是因为,User类并未给字段提供get/set方法,所以给前台传递过去的值是空的. 解决方案: 为User类添lombok的注解@Data,为实体类提供get/ ...
- Java中字符串转为16进制表示
Java中字符串转为16进制表示 String str = "鲸"; char[] chars = "0123456789ABCDEF".toCharArray ...
- 从SDCard获取的图片按分辨率处理的方法
前段时间公司开发的Launcher要做主题切换的功能,但切换主题时须要从sdcard中获取要切换的图片资源,拿到后图片的大小不正常. 后来查找原因是:系统对不同分辨率拿到的图片资源会自己主动的做转化, ...
- C#编译器选项(目标平台)
用vs编译C#项目的设置中,“属性-生成-目标平台”有anycpu,x86,x64等选项. anycpu(默认值)将编译程序集为使其在任意平台上都可以运行. 在任何可能的时候,应用程序作为 64 位进 ...
- ubuntu环境准备
一. 桌面方面看起来比较不爽,12的版本用起更不习惯,决定改成命令行登陆 a. vi /ect/default/grub 文件 b. 修改成第二个红框的情况 c. 执行update-grub命令 d ...
- python(27)- 面向对象练习Ⅰ
一:定义如下类,并最大程度地重用代码(继承,派生:子类重用父类方法,组合) 老师类 学生类 分数类 课程类 生日类 class People: def __init__(self,name,age,b ...
- 利用反射快速给Model实体赋值 使用 Task 简化异步编程 Guid ToString 格式知多少?(GUID 格式) Parallel Programming-实现并行操作的流水线(生产者、消费者) c# 无损高质量压缩图片代码 8种主要排序算法的C#实现 (一) 8种主要排序算法的C#实现 (二)
试想这样一个业务需求:有一张合同表,由于合同涉及内容比较多所以此表比较庞大,大概有120多个字段.现在合同每一次变更时都需要对合同原始信息进行归档一次,版本号依次递增.那么我们就要新建一张合同历史表, ...