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. [Fri 26 Jun 2015 ~ Thu 2 Jul 2015] Deep Learning in arxiv

    Natural Neural Networks Google DeepMind又一神作 Projected Natural Gradient Descent algorithm (PRONG) bet ...

  2. sap scriptfom 多语言翻译

    在某项目中,因为客户上线较早,非常多打印程序的form是由scriptform制做,又因为美国工厂要上线.免不了对scriptform进行多语言翻译.以下是对当中的一个交货单打印进行的多语言翻译,分享 ...

  3. php file_get_contents遇到https的处理办法

    最近调整了文件上传后的资源路径,导致pageOffice在线编辑功能无法正常使用,每一次打开文件都报错:该文件为0字节.仔细看了一下程序,整理逻辑流程如下图: 增加日志后发现,保存在服务器路径下的该条 ...

  4. 最小堆min_stack

    class MinStack {public: void push(int x) { ele.push(x); if(min.empty()||x<=min.top())    // in or ...

  5. 【译文】利用STAN做贝叶斯回归分析:Part 2 非正态回归

    [译文]利用STAN做贝叶斯回归分析:Part 2 非正态回归 作者 Lionel Hertzogn 前一篇文章已经介绍了怎样在R中调用STAN对正态数据进行贝叶斯回归.本文则将利用三个样例来演示怎样 ...

  6. 5200 fqy的难题----2的疯狂幂

    5200 fqy的难题----2的疯狂幂  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description ...

  7. 【最短路算法】Dijkstra知识点&代码

    代码: #include<iostream> #include<vector> #include<cstdio> #include<queue> #in ...

  8. Qt为啥从4.8直接就跳到5.3了呢?这不科学吧

    http://qt-project.org/downloads Qt 5.3 Select the file according to your operating system from the l ...

  9. 移动互联网App推广的十大难题

    常常有朋友来问."我做了一个App,请问怎么推广啊?"或者就是"我们公司开发了一个App.想短时间内获取巨大的量."还有的就是问"有没有什么好渠道三个 ...

  10. CodeForces - 735D Taxes (哥德巴赫猜想)

    Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...