python字符串
字符串格式化
字符串格式化使用字符串格式化操作符%来实现:格式化字符串 % 值(字符串或者数字或者多个值的元组,字典)
>>> format = "hello, %s. %s are a good gril"
>>> values = ('zyj' ,'you')
>>> print(format % values)
hello, zyj. you are a good gril
>>>
若字符串中包含%,则需要%%进行处理
>>> format1 = 'percent:%.2f%%'
>>> values = 10.123
>>> print(format1 % values)
percent:10.12%
>>>
可选参数:
参数1:转换标志:- 表示左对齐,默认为右对齐; + 表示在转换之前加上正负号; ''空白字符表示正数之前保留空格;0表示转换值若位数不够用0填充
参数2:最小字段宽度:转换后的字符串至少应该具有该值指定的宽度。如果是*,则宽度会从元组中提取
参数3:.后跟精度值:若为实数,精度值表示出现在小数点后的位数,若为字符串,那么该数字表示最大字段宽度。若是*精度将从元组中读出
>>> print('%x' % 16)
10
>>> print('%o' % 8)
10
>>> print('%s' % 'hello')
hello
>>> print('%r' % '42L')
'42L'
>>> from math import pi
>>> print('%10f' % pi)
3.141593
>>> print('%10.2f' % pi)
3.14
>>> print('%.2f' % pi)
3.14
>>> print('%10.5s' % 'hello,world')
hello
>>> format = '%*.*s'
>>> values = 'hello,world'
>>> print(format % (10,5,values))
hello
>>> print('%010.2f' % pi)
0000003.14
>>> print('%-10.2f' % pi)
3.14
>>> print(('% 5d' % 10) + '\n' +('% 5d' % -10))
10
-10
>>> print(('%+5d' % 10) + '\n' +('%+5d' % -10))
+10
-10
>>>
举例:在屏幕上打印九九乘法表:
print("九九乘法表:")
for x in range(1, 10):
for y in range(1, x + 1):
format1 = "%2d%s%-d%s%2d"
tuple1 = (x, ' * ', y, ' = ', x * y)
print(format1 % tuple1, end='')
print(" " * 4, end='')
print("\n") >>>
九九乘法表:
1 * 1 = 1 2 * 1 = 2 2 * 2 = 4 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 >>>
字符串方法
find :在较长的字符串中查找子串。返回子串所在位置的最左端索引。如果没有找到返回-1
此方法可以接收可选的起始点和结束点参数;注意:包含第一个索引,但不包含第二个索引
>>> subject = "hello,world"
>>> print(subject.find('world',2))
6
>>>
join:用来连接序列中的元素,需要被连接的序列元素都必须是字符串。
>>> dirs ='','usr','bin','env'
>>> print('/'.join(dirs))
/usr/bin/env
>>> print('\\'.join(dirs))
\usr\bin\env
>>> ipAddr = ['12','1','1','1']
>>> netMask = ['255','255','255','0']
>>> print('.'.join(ipAddr)+' '+'.'.join(netMask))
12.1.1.1 255.255.255.0
>>>
split:将字符串分割成序列,方法中可以不提供分隔符。程序会把所有的空格作为分隔符
>>> print("I love you".split())
['I', 'love', 'you']
>>> print(list("I love you"))
['I', ' ', 'l', 'o', 'v', 'e', ' ', 'y', 'o', 'u']
>>> print(tuple("I love you"))
('I', ' ', 'l', 'o', 'v', 'e', ' ', 'y', 'o', 'u')
>>> print('1+2-3+5'.split('+'))
['1', '2-3', '5']
>>>
lower:返回字符串的小写字母,在‘不区分大小写’的时候,进行存储和查找。
>>> print(string1.lower())
hello,world
>>> print('hello' in string1.lower())
True
>>>
title:字符串中的所有字母的首字母大写。
>>> title1 = 'jave script'
>>> print(title1.title())
Jave Script
>>>
string模块的capwords函数
>>> import string
>>> print(string.capwords("jave script"))
Jave Script
>>>
replace:返回某字符串的所有匹配项均被替换之后得到的字符串
>>> string2 = 'this is a test'
>>> print(string2.title().replace('is','at'))
That Is A Test
>>> print(string2)
this is a test
>>>
strip:返回去除字符串两侧空格,可以增加参数以去掉特定的字符
>>> string3 = ' show memory -more- '
>>> print(string3.strip())
show memory -more-
>>> psws = ['a','b']
>>> psw = 'a '
>>> print(psw in psws)
False
>>> print(psw.strip() in psws)
True
>>> string4 ='####!!!!*****happy birthday to you*****####!!!!'
>>> print(string4.title().strip('#!'))
*****Happy Birthday To You*****
>>> print(string4.title().rstrip('!#'))
####!!!!*****Happy Birthday To You*****
>>> print(string4.title().lstrip('!#'))
*****Happy Birthday To You*****####!!!!
>>> string5 ='####!!!!###!!!####!!!!'
>>> print(string5.strip('#!')) >>>
translate:替换字符串中的某些部分,只能处理单个字符;如替换因平台而异的特殊字符。
python字符串的更多相关文章
- 关于python字符串连接的操作
python字符串连接的N种方式 注:本文转自http://www.cnblogs.com/dream397/p/3925436.html 这是一篇不错的文章 故转 python中有很多字符串连接方式 ...
- StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?
StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...
- Python 字符串
Python访问字符串中的值 Python不支持单字符类型,单字符也在Python也是作为一个字符串使用. Python访问子字符串,可以使用方括号来截取字符串,如下实例: #!/usr/bin/py ...
- python字符串方法的简单使用
学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...
- python字符串基础知识
1.python字符串可以用"aaa",'aaa',"""aaa""这三种方式来表示 2.python中的转义字符串为" ...
- Python 字符串格式化
Python 字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存 一 ...
- Python 字符串操作
Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip() .lstrip() .rstrip(',') 复制字符 ...
- 【C++实现python字符串函数库】strip、lstrip、rstrip方法
[C++实现python字符串函数库]strip.lstrip.rstrip方法 这三个方法用于删除字符串首尾处指定的字符,默认删除空白符(包括'\n', '\r', '\t', ' '). s.st ...
- 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- 【C++实现python字符串函数库】一:分割函数:split、rsplit
[C++实现python字符串函数库]split()与rsplit()方法 前言 本系列文章将介绍python提供的字符串函数,并尝试使用C++来实现这些函数.这些C++函数在这里做单独的分析,最后我 ...
随机推荐
- 如何让ConfigurationManager打开任意的配置文件
VisualStudio的配置文件很好很强大,用来保存数据库连接字符串或键值对都非常方便,只需要通过ConfigurationManager的ConnectionStrings或AppSettings ...
- Excel 自动更正
当有复杂的字段需要重复填写怎么办呢,比如××银行卡号,××电话号码,××公司地址等.可以使用excel的"自动更正"功能解决. 1. Excel 2010的自动更正选项在哪里呢 2 ...
- 自定义PHP系统异常处理类
<?php // 自定义异常函数 set_exception_handler('handle_exception'); // 自定义错误函数 set_error_handler('handle_ ...
- CentOS Linux系统下更改Apache默认网站目录
引言: Apache默认的网站目录是在/var/www/html,我们现在要把网站目录更改到/home/wwwroot/web1/htdocs,操作如下 准备工作: 创建目录: cd /home mk ...
- LINUX DNS解析的3种修改方法~
1.HOST 本地DNS解析 vi /etc/hosts 添加规则 例如: 223.231.234.33 www.baidu.com 2.网卡配置文件DNS服务地址 vi /etc/sysconfi ...
- twemproxy explore,redis和memcache代理服务器
twemproxy,也叫nutcraker.是一个twtter开源的一个redis和memcache代理服务器. redis作为一个高效的缓存服务器,非常具有应用价值.但是当使用比较多的时候,就希望可 ...
- js中的事件委托或是事件代理详解
起因: 1.这是前端面试的经典题型,要去找工作的小伙伴看看还是有帮助的: 2.其实我一直都没弄明白,写这个一是为了备忘,二是给其他的知其然不知其所以然的小伙伴们以参考: 概述: 那什么叫事件委托呢?它 ...
- java 深入技术六(Map)
Map 1.map概述 map.put(key,value)里面存放的是两个相关的数据,key=value键值对 Map集合中存放的是键值对(put(key,value)),用get(key)获取集合 ...
- maven install 构建报错(2)
错误:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin: 2.3 . 2 :compile ( default ...
- apache通过cgi调用exe程序
windows下,使用c写了一个简单的cgi程序,生成exe类型的可执行文件,代码如下: #include<stdio.h> int main() { printf("Conte ...