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' ...
随机推荐
- zzuli 2179 最短路
2179: 紧急营救 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 89 Solved: 9 SubmitStatusWeb Board Descri ...
- 【scala】应用程序和App特性
一.应用程序 要运行一个Scala对象,必须提供一个独立对象的名称.这个独立对象需要包含一个main方法,该方法接受一个Array[String]作为参数,结果类型为Unit. import Chec ...
- .SourceInsight添加.S文件
在Option->Document Option添加配置.S然后再去添加文件
- CSS:Tutorial one
1.Three Ways to Insert CSS External style sheet Internal style sheet Inline style External Style She ...
- react: next-redux-saga
instead of using the Provider component, you can use the withRedux higher order component to inject ...
- 剑指offer--29.从上往下打印二叉树
层序遍历,队列 ------------------------------------------------------------------------------------- 时间限制:1 ...
- 2017.11.2 Talk to customers for an hour
yesterday::: Hi Huang, For the better performance of the test the Con 6 should be connected all the ...
- Android 之Navicat for SQLite 数据库介绍
Navicat for SQLite 是一套专为SQLite 设计的强大数据库管理及开发工具 Navicat for SQLite 是一套专为SQLite 设计的强大数据库管理及开发工具.它可以 ...
- linux多线程全面解析
引入: 在传统的Unix模型中,当一个进程需要由另一个实体执行某件事时,该进程派生(fork)一个子进程,让子进程去进行处理.Unix下的大多数网络服务器程序都是这么编写的,即父进程接受连 ...
- uid
var uid = 0 function nextUid() { return ++uid }