1、去空格和换行符:

s = '  a   bc   '
print(s.strip())#strip(),去掉字符串两边的空格和换行符,无法去除中间的空格
print(s.rstrip())#rstrip(),去掉右边的空格
print(s.lstrip())#lstrip(),去掉左边的空格

2、替换:

print(s.replace('a','A')) #把a替换为A,返回一个新的字符串,只替换不会更改原字符串
print(s.replace(' ',''))#把空格替换为空字符串

3、找下标:

s = 'abc'
print(s.index('c')) #找下标
print(s.find('a')) #找下标,若元素存在则同index
print(s.find('e')) #找不存在的下标,返回-1,用find
# print(s.index('f'))#找不存在的下标,报错ValueError

4、大小写

print(s.upper()) #把所有字母都变为大写
print(s.lower()) #把所有字母都变为小写
print(s.capitalize())#首字母大写,其余小写
print(s.title())#标题化,所有单词的首字母大写

5、补齐

print(s.center(50,'='))#把原字符串放在中间,若不够50,则将补齐50
print(s.center(5,'*'))

6、统计次数

print(s.count('c')) #找某个字符出现的次数

7、补零

s2 = ''
print(s2.zfill(5)) #在前面补0,补5-1个0

8、各种判断 

print(s.startswith('a'))#true ,判断是否已xx开头
print(s.endswith('.jpg'))#false,判断是否已xx结尾
print(s.isdigit())#判断字符串里存的是否为整数
print(s2.islower())#是不是全是小写字母 ,数字应都是false
print(s2.isupper())#是不是全是大写字母
print(s2.isalpha())#是字母或汉字,全都返回true
print(s2.isalnum())#只有数字或字母或汉字会返回true,其他的全返回false(用于不允许输入特殊字符的情况)
print(s2.isspace())#判断是否都是空格

9、字符串格式化

s3 = '今天是{},欢迎{}登录'
s4 = 'insert into stu (id,username,passwd,phone) value ("{id}","{username}","{password}","{phone}")' print(s3.format('','小明')) #做字符串格式化的
print(s4.format(username = 'abc',id = 1,password = 'abc12323',qq = '',phone = ''))
print(s4.format_map({"username":"abc",'id':1,"password":"dfasdf","phone":''}))#传一个字典

10、分割字符串(常用)

stus='xiaoming,xiaohei,xiaobai,jaojun'
print(stus.split(',')) #分割字符串,常用!!! 以逗号分割
example = 'a b c d ef 123'
print(example.split())#什么都不写,则以空格分割
print(example.split('.'))#没有句号 则将字符串放到一个list里

11、连接字符串(常用)

l = ['xiaoming', 'xiaohei', 'xiaobai', 'jaojun']

print(','.join(l)) #把list里面的每一个元素通过指定的字符串连接起来
print(' '.join(l)) #用空格把元素连接起来
												

Python 字符串常用方法 day2的更多相关文章

  1. python 字符串常用方法

    字符串常用方法 capitalize() String.capitalize() 将字符串首字母变为大写 name = 'xiaoming' new_name = name.capitalize() ...

  2. Python字符串常用方法(二)

    二.字符串的操作常用方法 字符串的替换.删除.截取.复制.连接.比较.查找.分割等 1. string. lower() :转小写 2. string. upper() :转大写 3. string. ...

  3. python字符串常用方法、分割字符串等

    一.字符串的常用方法 1.str.capitalize()  字符串首字母大写 2.str.center()  把字符串居中 3.str.isalnum() 判断字符串是否含有英文.数字,若有英文和数 ...

  4. Python字符串常用方法(一)

    一.字符串的判断常用方法 字符串的字母,数字,大小写,空格等的判断 1.string. isalnum() :(字母数字判断) 如果 string 至少有一个字符并且所有字符都是字母或数字则返回 Tr ...

  5. python字符串常用方法

    #1.strip()去掉空格(字符串首.尾空格).lstrip()去掉左侧空格.rstrip()去掉右侧空格print(' abc '.lstrip())#>>abcprint(' abc ...

  6. Python 字符串常用方法总结

    明确:对字符串的操作方法都不会改变原来字符串的值 1,去掉空格和特殊符号 name.strip()  去掉空格和换行符 name.strip('xx')  去掉某个字符串 name.lstrip()  ...

  7. python 字符串 常用方法

    name = 'ALLix9' print(name.casefold()) # 大写变成小写 name.lower() # 全变小写 '.isnumeric()) #判断是否是数字:正整数 prin ...

  8. python基础(2)字符串常用方法

    python字符串常用方法 find(sub[, start[, end]]) 在索引start和end之间查找字符串sub ​找到,则返回最左端的索引值,未找到,则返回-1 ​start和end都可 ...

  9. Python之旅Day2 元组 字符串 字典 集合

    元组(tuple) 元组其实跟列表差不多,也是存一组数,与列表相比,元组一旦创建,便不能再修改,所以又叫只读列表. 语法: names = ("Wuchunwei","Y ...

随机推荐

  1. firedac的TFDStoredProc动态创建并调用存储过程

    1)中间件执行存储过程 sp.Close; sp.StoredProcName := procName; sp.Prepare;  // 生成存储过程的参数列表,无任何OUTPUT的存储过程,也会自动 ...

  2. Mac OSX:安装zsh

    想在mac下安装oh my zsh,按照https://github.com/robbyrussell/oh-my-zsh上的文档,执行下面这条命令安装:curl -L http://install. ...

  3. mysql无密码重启

    mysql无密码重启 /etc/init.d/mysql stopnohup /usr/bin/mysqld_safe --user=mysql --skip-grant-tables &

  4. Apache OFbiz service engine 源代码解读

    上一篇看完了ofbiz entity engine,这篇再来过一下ofbiz的service engine.service engine层在设计模式的使用上跟entity engine有些相似,最典型 ...

  5. HDU 5294 Tricks Device(多校2015 最大流+最短路啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 Problem Description Innocent Wu follows Dumb Zha ...

  6. ibatis 一对多查询

    <typeAlias alias="businessScopeItem" type="com.sdfrdj.vo.BusinessScopeItem"/& ...

  7. oc33--构造方法2

    // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property int age; ...

  8. CodeForces 754D Fedor and coupons&&CodeForces 822C Hacker, pack your bags!

    D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  9. [BZOJ 1718] Redundant Paths

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1718 [算法] 用Tarjan算法找出所有e-DCC(边-双联通分量),然后将这张图 ...

  10. 洛谷 P3515 [ POI 2011 ] Lightning Conductor —— 决策单调性DP

    题目:https://www.luogu.org/problemnew/show/P3515 决策单调性... 参考TJ:https://www.cnblogs.com/CQzhangyu/p/725 ...