Python——string
字符串操作
string典型的内置方法:
- count()
- center()
- startswith()
- find()
- format()
- lower()
- upper()
- strip()
- replace()
- split()
- join()
count()
计数,查询字符串中出现指定字符的次数。
st='hello kitty'
print(st.count('l'))
输出:2
center()
字符串居中。其中,50表示新字符串长度,'#'表示填充字符。
st='hello kitty'
print(st.center(50,'#'))
输出:
###################hello kitty####################
startswith()
判断是否以某个内容开头。
st='hello kitty'
print(st.startswith('he'))
输出:True
find()
查找到第一个元素,并将索引值返回。
st='hello kitty'
print(st.find('t'))
输出:8
format()
格式化输出的一种方式。(另一种方式在字符串中加%d、%s、%f,字符串外加%变量名)
st='hello kitty {name} is {age}'
print(st.format(name='alex',age=37))
输出:
hello kitty alex is 37
lower()
将字符串中的大写全部变成小写。
print('My tLtle'.lower())
输出:
my tltle
upper()
将字符串中的小写全部变成大写。
print('My tLtle'.upper())
输出:
MY TLTLE
strip()
将字符串中的制表符、换行符等不可见字符去掉。
print('\tMy tLtle\n'.strip())
输出:
My tLtle
replace()
将字符串中第1个'itle'替换为'lesson'。
print('My title title'.replace('itle','lesson',1))
输出:
My tlesson title
split()
以字符串中的第1个i为分隔符,将字符串分割为两部分,并放入列表中。
print('My title title'.split('i',1))
输出:
['My t', 'tle title']
详细解释:

join()
字符串拼接最好的方法。需要注意的是,join()中必须是一个列表。
str1 = 'Alex'
str2 = 'Oliver'
a = ''.join([str1,' and ',str2])
print(a)
输出:
Alex and Oliver
Python查看帮助的方法
>>> help(str)
>>> help(str.split)
>>> help(int)
小练习
统计str字符串中每个字符的个数并打印。
str = 'U2FsdGVkX1+xaZLZ3hGZbZf40vPRhk+j+FHIHiopidA8GG6aolpjAU7kVHuLMYcJ'
# 方法1:
b = {}
for i in str:
if i in b:
b[i] += 1
else:
b.setdefault('%s'%i,1)
for i,v in b.items():
print("字符串:%s,个数:%d"%(i,v))
#方法2
dic = {}
for i in str:
if i in dic:continue
else:
dic.setdefault(i,str.count(i))
print(i,str.count(i))
Python——string的更多相关文章
- python string module
String模块中的常量 >>> import string >>> string.digits ' >>> string.letters 'ab ...
- python string
string比较连接 >>> s1="python string" >>> len(s) 13 >>> s2=" p ...
- The internals of Python string interning
JUNE 28TH, 2014Tweet This article describes how Python string interning works in CPython 2.7.7. A fe ...
- Python string objects implementation
http://www.laurentluce.com/posts/python-string-objects-implementation/ Python string objects impleme ...
- Python string replace 方法
Python string replace 方法 方法1: >>> a='...fuck...the....world............' >>> b=a ...
- Python string interning原理
原文链接:The internals of Python string interning 由于本人能力有限,如有翻译出错的,望指明. 这篇文章是讲Python string interning是如何 ...
- python string与list互转
因为python的read和write方法的操作对象都是string.而操作二进制的时候会把string转换成list进行解析,解析后重新写入文件的时候,还得转换成string. >>&g ...
- python string 文本常量和模版
最近在看python标准库这本书,第一感觉非常厚,第二感觉,里面有很多原来不知道的东西,现在记下来跟大家分享一下. string类是python中最常用的文本处理工具,在python的 ...
- Python String 方法详解
官网文档地址:https://docs.python.org/3/library/stdtypes.html#string-methods 官网 公号:软测小生ruancexiaosheng 文档里的 ...
- python string/list转换
python的read.write方法的操作对象都是string.输入.输出和逻辑业务上很多时候都要用到string.list互转. 1.简单用法 import stringstr = 'abcde' ...
随机推荐
- WAF:web应用防火墙
1,sql注入2,xss3,不安全下载 code_backup.tar.gz .sql 4.隐私文件访问 .svn .git 5.弱口令6. 非授权访问 redis 7.cc攻击 性能cc攻击8.DD ...
- Linux:配置Linux网络和克隆虚拟机并更改配置
Linux学习笔记1:配置Linux网络和克隆虚拟机并更改配置 一.配置Linux网络 在安装Linux的时候,一定要保证你的物理网络的IP是手动设置的,要不然会在Linux设置IP连通网络的时候 ...
- cylance做的机器学习相关材料汇总
https://www.cylance.com/en_us/products/our-products/protect----threatzero.html 产品介绍 关键!!!! https://w ...
- angularJS中directive父子组件的数据交互
angularJS中directive父子组件的数据交互 1. 使用共享 scope 的时候,可以直接从父 scope 中共享属性.使用隔离 scope 的时候,无法从父 scope 中共享属性.在 ...
- 【转】Symstore 详细使用
SymStore (symstore.exe) 是用于创建符号存储的工具.它被包含在Windows调试工具包中. SymStore按照某种格式存储符号,使得调试器可以通过时间戳.映像大小(对于.dbg ...
- layui中实现上传图片压缩
一.关于js上传图片压缩的方法,百度有很多种方法,这里我参考修改了一下: function photoCompress(file, w, objDiv) { var ready = new FileR ...
- LeetCode OJ:Maximum Subarray(子数组最大值)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Kotlin 第一弹:自定义 ViewGroup 实现流式标签控件
古人学问无遗力, 少壮工夫老始成.纸上得来终觉浅, 绝知此事要躬行. – 陆游 <冬夜读书示子聿> 上周 Google I/O 大会的召开,宣布了 Kotlin 语言正式成为了官方开发语言 ...
- 基于Photon 的 PUN+ 如何自动实现RPC呼叫的.
基于Photon 的 PUN+ 如何自动实现RPC呼叫的. 简单说函数标记成 RPC 类型的. void Hello() { Debug.Log("Hello"); PhotonV ...
- 基于UDP协议编程
基于udp套接字 udp是无链接的,先启动哪一端都不会报错. UDP(user datagram protocol,用户数据报协议)是无连接的,面向消息的,提供高效率服务.不会使用块的合并优化算法,, ...