python(4)---字符串常用方法

python字符串常用方法

password=‘.jpg 123456789 .jpg ABCDFG\n  ‘  #字符串的值不能改变

##strip()方法
print(password.strip())  #默认去掉字符串两边的空格和换行符
print(password.strip(‘.jpg‘)) #去掉两边的.jpg
print(password.lstrip())   #掉字符串左边的空格和换行符
print(password.rstrip())   #掉字符串右边的空格和换行符

##大小写转换
print(password.upper())  #转成大写
print(password.lower())  #转成小写
print(password.capitalize()) #首字母改成大写

##替换
print(password.replace(‘123‘,‘hehe‘)) #替换
print(password.replace(‘ ‘,‘‘))  #去掉字符串中的所有空格

## 开头、结尾
filename=‘a.mp3‘
print(filename.endswith(‘.mp3‘))  #判断是否以XX结尾
print(filename.startswith(‘186‘)) #判断是否以XX开头

##字符串分割、转为list
names=‘晨希,瑞涵,提子,库里‘
print(names.split())        # 把字符串变成list,如果不指定用什么分割,默认用空格分割;
print(names.split(‘.‘))    # 若分隔符不存在,把整个字符串当做一个元素放到list中
print(names.split(‘,‘))   #1、把字符串变成list  2、以某个元素分割,分割以后是list里的每个元素
print(list(names))

原文地址:https://www.cnblogs.com/HathawayLee/p/9537572.html

时间: 2024-08-12 12:49:36

python(4)---字符串常用方法的相关文章

python之字符串常用方法

1.字符串常用方法print(s.zfill(4)) #在前面补0print(s.strip())print(s.lstrip())print(s.rstrip())print(s.replace('a','A')) #替换print(s.count('c'))#统计出现次数print(s.index('c')) #找下标.找不到报错print(s.find('d')) #找下标,找不到返回-1print(s.startswith('a')) #判断是否a开始print(s.endswith('

Python入门-字符串常用方法

Python 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可. var1 = 'Hello World!' var2 = "Python Runoob" 原文地址:https://www.cnblogs.com/zivli/p/9485120.html

python 数据结构 - 字符串

字符串是 Python 中最常用的数据类型.我们可以使用单引号 ( '' ) 或双引号 ( " " ) 来创建字符串. 在python中,创建变量不需要指定类型,你在创建变量赋值时,python解释器会自动推断变量的数据类型 1 >>> s = 'abcde' 2 >>> type(s) 3 <class 'str'> #这里把多种数据类型的值赋值给s,python都能自动识别 4 >>> s = 123 5 >&

Python中字符串的使用

这篇文章主要介绍python当中用的非常多的一种内置类型——str.它属于python中的Sequnce Type(序列类型).python中一共7种序列类型,分别为str(字符串),unicode(u字符串),list(列表),tuple(元组),bytearray(字节数组),buffer(缓冲内存),xrange(范围).它们的通用操作如下: Operation Result x in s 判断x是否在s中 x not in s 判断x是不在s中 x + t 两个序列合并, 将t加到s之后

Python 之字符串常用操作

字符串表示:str与repr的区别str()函数把值转换为合理形式的字符串,便于理解repr()函数是创建一个字符串,以合法的Python表达式形式来表示值.如下: #-*-encoding:utf-8-*- print repr("hello repr") print str("hello str") 运行结果: 'hello repr' hello str 字符串格式化: 字符串格式化转换类型 转换类型 含义 d ,i 带符号的十进制整数 o 不带符号的八进制

python学习—字符串

字符串拼接 s1="python" s2="hello" 1.+号 s3=s1+s2 2.join方法 obj.join() j=" " s4= j.join((s1,s2))  ---链接顺序:s1 j s2 字符串格式化输出 1.format name=input("输入名字:") a1="今天收到{},交来{},金额{}".format(name, "学费", 666) {:.3f

python time模块常用方法小结

Python time 模块常用方法小结 本文旨在记录 python 中 time 模块常用的方法. 1. time 模块常用的方法 获取当前系统时间的时间戳 import time # 返回当前时间的时间戳 time.time() 时间元组的概念 获取时间元组的方法: 接收时间戳(1970纪元后经过的浮点秒数)并返回当地时间下的时间元组t(t.tm_isdst可取0或1,取决于当地当时是不是夏令时). time.localtime(time.time()) # 时间元组 time.struct

Python 的字符串类子串查找函数

Python 的字符串类有个很好用的函数,可很方便的用于与查找Python字符串类型对象子串相关的操作,具体的API如下: | find(...) | S.find(sub [,start [,end]]) -> int | | Return the lowest index in S where substring sub is found, | such that sub is contained within s[start:end]. Optional | arguments start

python中字符串链接的七种方式

一. str1+str2 string类型 '+'号连接 >>> str1="one" >>> str2="two" >>> str1+str2 'onetwo' >>>注意:该方式性能较差,因为python中字符串是不可变的类型,使用 + 连接两个字符串时会生成一个新的字符串,生成新的字符串就需要重新申请内存,当连续相加的字符串很多时(a+b+c+d+e+f+...) ,效率低下就是必然的了例