字符串操作

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的更多相关文章

  1. python string module

    String模块中的常量 >>> import string >>> string.digits ' >>> string.letters 'ab ...

  2. python string

    string比较连接 >>> s1="python string" >>> len(s) 13 >>> s2=" p ...

  3. The internals of Python string interning

    JUNE 28TH, 2014Tweet This article describes how Python string interning works in CPython 2.7.7. A fe ...

  4. Python string objects implementation

    http://www.laurentluce.com/posts/python-string-objects-implementation/ Python string objects impleme ...

  5. Python string replace 方法

    Python string replace   方法 方法1: >>> a='...fuck...the....world............' >>> b=a ...

  6. Python string interning原理

    原文链接:The internals of Python string interning 由于本人能力有限,如有翻译出错的,望指明. 这篇文章是讲Python string interning是如何 ...

  7. python string与list互转

    因为python的read和write方法的操作对象都是string.而操作二进制的时候会把string转换成list进行解析,解析后重新写入文件的时候,还得转换成string. >>&g ...

  8. python string 文本常量和模版

        最近在看python标准库这本书,第一感觉非常厚,第二感觉,里面有很多原来不知道的东西,现在记下来跟大家分享一下.     string类是python中最常用的文本处理工具,在python的 ...

  9. Python String 方法详解

    官网文档地址:https://docs.python.org/3/library/stdtypes.html#string-methods 官网 公号:软测小生ruancexiaosheng 文档里的 ...

  10. python string/list转换

    python的read.write方法的操作对象都是string.输入.输出和逻辑业务上很多时候都要用到string.list互转. 1.简单用法 import stringstr = 'abcde' ...

随机推荐

  1. idea maven打包 install 报错The packaging for this project did not assign a file to the build artifact

    如题,这其实是个低级错误,这个错的意思是,找不到这个插件的包. 原因很简单,不是找不到这个打包插件,而是自己的项目没有从maven仓库里加载这个包到项目里,因此会找不到. 看一下问什么会报这个错: 大 ...

  2. CC攻击工具list

    从论文里抠出来的工具列表如下,后面有黑产的工具以及网络上摘录的工具: 分类:(1)有僵尸网络(是否代理服务器)&没有的==>(2)单一url&混合url(多线程,压测为主,dem ...

  3. 【scala】apply和update

    我们在使用scala的时候经常会用到对象的apply方法和update方法. 虽然我们表面没有察觉,但是实际上两个方法都会遵循相关约定被调用. apply apply方法的约定:用括号传递给变量(对象 ...

  4. 【转载】Git,Github和Gitlab简介和基本使用Gitlab安装和使用

    http://blog.csdn.net/u011241606/article/details/51471367

  5. LeetCode OJ:Pascal's Triangle(帕斯卡三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  6. Agilent RF fundamentals (5)

    2考虑两个因素 3 RX 4 TX RX switch use Duplexer

  7. Android Issue分析方法(用anr来说明)

    Log的产生大家都知道 , 大家也都知道通过DDMS来看log , 但什么时候会产生log文件呢 ?一般在如下几种情况会产生log文件 . 1,程序异常退出 , uncaused exception ...

  8. WampServer常用环境配置

    WampServer即Windows Apache Mysql PHP集成安装环境,就是在window下的apache.php和mysql的服务器软件. 其特点就是 1.支持中文语言,一键安装,省时省 ...

  9. c语言编译执行过程

    <h4>认识C编译执行过程</h4>认识C编译执行过程,是C学习的开端.简单说C语言从编码编译到执行要经历一下过程: C源代码编译---->形成目标代码,目标代码是在目标 ...

  10. sqlalchemy的基本的使用

    参考链接:http://www.techweb.com.cn/network/system/2016-10-11/2407638.shtml http://www.cnblogs.com/renfan ...