字符串操作

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. 四边形不等式优化_石子合并问题_C++

    在动态规划中,经常遇到形如下式的状态转移方程: m(i,j)=min{m(i,k-1),m(k,j)}+w(i,j)(i≤k≤j)(min也可以改为max) 上述的m(i,j)表示区间[i,j]上的某 ...

  2. ios通知使用 书上案例 简单易懂

    /* The notification name */const NSString *ResultOfAppendingTwoStringsNotification =@"ResultOfA ...

  3. CentOS7 安装ifconfig

    As we all know, “ifconfig” command is used to configure a network interfaces in GNU/Linux systems. I ...

  4. Win7 x64安装Paramiko

    先说一下我的环境: win7 x64 旗舰版.Python3.5.0.pip8.1.0 pip install paramiko时报错如下: 大概意思: blablabla... 反正大概意思就是少G ...

  5. 《Java程序员职场全攻略 从小工到专家》 - 书摘精要

    (前言) 学习招式在次,提升内力才是最主要的: (P10) 选择一门编程语言,只是入门的途径.过分依赖编程语言,只会让自己成为代码高手,而不是开发大牛,要知道编程语言只是一种工具,更重要的是编程思想: ...

  6. 基于用户登陆的struts2中action的分类详解

    在struts2中action的分类有:继承 ActionSupport 实现 Action,模型驱动(ModelDriven)的 Action,多方法的 Action三种方式. 1.继承 Actio ...

  7. 类Flask实现前后端交互之代码聊天室

    前言 框架 项目目录及各自功能 流程图 后端 server backend exector 前端 ajax 页面更新 演示 简易应答模式 代理模式处理外部请求 后台日志 总结 前言 这两天老是做梦,全 ...

  8. 可视化CNN神经网路第一层参数

    在上Andrew Ng的课的时候搜集到了课程里面自带的显示NN参数的代码,但是只能显示灰度图,而且NN里的参数没有通道的概念.所以想要获得可视化CNN的参数,并且达到彩色的效果就不行了. 所以就自己写 ...

  9. 使用tor实现匿名扫描/SSH登录

    你要做坏事时,最先应该想到匿名.扫描网站/主机,或利用漏洞:甚至在大天朝发帖都有风险,为了防止半夜鬼敲门,我们可以使用tor实现匿名. 如果你不知道tor是什么,看:https://zh.wikipe ...

  10. LINUX 命令—netstat [简单实用]

    1.--当我们在检查程序是否启动或者网络状况的时候 会查看本机活跃的端口,就需要这个命令: |--"netstat – Print network connections, routing ...