python之字符串函数
1. endswith() startswith()
# 以什么什么结尾
# 以什么什么开始
test = "alex"
v = test.endswith('ex')
v = test.startswith('ex')
print(v)
2. expandtabs()
test = "1\t2345678\t9"
v = test.expandtabs(6)
print(v,len(v))
3. find()
# 从开始往后找,找到第一个之后,获取其未知
# > 或 >=
test = "alexalex"
# 未找到 -1
v = test.find('e')
print(v)
4. index()
# index找不到,报错 忽略
test = "alexalex"
v = test.index('a')
print(v)
5. format() format_map()
# 格式化,将一个字符串中的占位符替换为指定的值
test = 'i am {name}, age {a}'
print(test)
v = test.format(name='alex',a=19)
print(v) test = 'i am {0}, age {1}'
print(test)
v = test.format('alex',19)
print(v) # 格式化,传入的值 {"name": 'alex', "a": 19}
test = 'i am {name}, age {a}'
v1 = test.format(name='df',a=10)
print(v1)
v2 = test.format_map({"name": 'alex', "a": 19})
print(v2)
6. isalnum()
# 字符串中是否只包含 字母和数字
test = "er;"
v = test.isalnum()
print(v)
7.capitalize() 首字母大写
test = "sqy"
v = test.capitalize()
print(v)
8. casefold() lower() 所有变小写,casefold更牛逼,很多未知的对相应变小写
test = "sqy"
v1 = test.casefold()
print(v1)
v2 = test.lower()
print(v2)
9. center() 置宽度,并将内容居中
# 20 代指总长度
# * 空白未知填充,一个字符,可有可无
test = "sqy"
v = test.center(20,"中")
print(v)
10. ljust() rjust() zfill()
# test = "alex"
# v = test.ljust(20,"*")
# print(v) # test = "alex"
# v = test.rjust(20,"*")
# print(v) # test = "alex"
# v = test.zfill(20)
# print(v)
11. count() 去字符串中寻找,寻找子序列的出现次数
test = "aLexalexr"
v = test.count('ex')
print(v) test = "aLexalexr"
v = test.count('ex',5,6)
print(v)
12. isalpha() 是否是字母,汉子
test = "as大多数df"
v = test.isalpha()
print(v)
13. isdecimal() isdigit() isnumeric() 当前输入是否是数字
test = "②" # 1,②,儿
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)
14.isprintable() 是否存在不可显示的字符
# \t 制表符
# \n 换行
test = "gdds\tfdsfd"
v = test.isprintable()
print(v)
15. isspace() 判断是否全部是空格
test = " ass"
v = test.isspace()
print(v)
16. istitle() title() 判断是否是标题
test = "Return True if all cased characters in S are uppercase and there is"
v1 = test.istitle()
print(v1)
v2 = test.title()
print(v2)
v3 = v2.istitle()
print(v3)
17. join() 将字符串中的每一个元素按照指定分隔符进行拼接
test = "你是风儿我是沙"
print(test)
t = ' '
v = "_".join(test)
print(v)
18. islower() lower() isupper() upper()
test = "Alex"
v1 = test.islower()
v2 = test.lower()
print(v1, v2) v1 = test.isupper()
v2 = test.upper()
print(v1,v2)
19. lstrip() rstrip() strip() 移除指定字符串 有限最多匹配
test = "\nxas12xa\n\txax1221axa "
v = test.lstrip('xa')
v = test.rstrip('9lexxexa')
v = test.strip('xa')
print(v) test.lstrip()
test.rstrip()
test.strip()
# 去除左右空白
v = test.lstrip()
v = test.rstrip()
v = test.strip()
print(v)
print(test)
# 去除\t \n
v = test.lstrip()
v = test.rstrip()
v = test.strip()
print(v)
20. maketrans() translate() 对应关系替换
v = "asidufkasd;fiuadkf;adfkjalsdjf"
m = str.maketrans("aeiou", "12345")
new_v = v.translate(m)
print(new_v)
21. partition() rpartition() 分割为三部分
test = "testasdsddfg"
v = test.partition('s')
print(v)
v = test.rpartition('s')
print(v)
22. split() rsplit() splitlines()
test = "sqys"
v = test.split('s',2)
print(v)
v = test.rsplit()
print(v) # 23 分割,只能根据,true,false:是否保留换行
# test = "asdfadfasdf\nasdfasdf\nadfasdf"
# v = test.splitlines(False)
# print(v)
23. swapcase() 大小写转换
test = "aLex"
v = test.swapcase()
print(v)
24. isidentifier() 字母,数字,下划线 : 标识符 def class
a = "de12_f"
v = a.isidentifier()
print(v)
25.replace() 将指定字符串替换为指定字符串
test = "alexalexalex"
v = test.replace("ex",'bbb')
print(v)
v = test.replace("ex",'bbb',2)
print(v)
26.灰魔法
一、for循环
test = "返回到合肥东方红刚才修改"
# for 变量名 in 字符串:
# 变量名
# break
# continue index = 0
while index < len(test):
v = test[index]
print(v) index += 1
print('=======') for zjw in test:
print(zjw) test = "好方法个地方个地方刚发的"
for item in test:
print(item)
break for item in test:
continue
print(item)
二、索引,下标,获取字符串中的某一个字符
test = "好方法个地方个地方刚发的"
v = test[3]
print(v)
三、切片
test = "好方法个地方个地方刚发的"
v = test[0:10] # 0=< <1
print(v)
四、获取长度
# Python3: len获取当前字符串中由几个字符组成
test = "好方法个地方个地方刚发的"
v = len(test)
print(v)
27. range() 获取连续或不连续的数字
# Python2中直接创建在内容中
# python3中只有for循环时,才一个一个创建
v = range(10)
v = range(3,10)
v = range(1,10,2)
# 帮助创建连续的数字,通过设置步长来指定不连续
v = range(0, 100, 5) for item in v:
print(item)
28.zip()
# print(list(zip(('a','n','c'),(1,2,3))))
# print(list(zip(('a','n','c'),(1,2,3,4))))
# print(list(zip(('a','n','c','d'),(1,2,3))))
#
# p={'name':'alex','age':18,'gender':'none'}
# print(list(zip(p.keys(),p.values())))
# print(list(p.keys()))
# print(list(p.values()))
#
print(list(zip(['a','b','c'],'')))
python之字符串函数的更多相关文章
- Python的字符串函数
今天用了将近一天的时间去学习Python字符串函数 上午学了17个,下午学了23个(共计40) 详细内容请见菜鸟教程--Python3字符串--Python的字符串内建函数
- python(字符串函数)
一.字符串函数 1.首字母大小写 capitalize() title() name = "xinfangshuo" print (name.capitalize()) print ...
- python笔记-字符串函数总结
字符串函数: chr() 数字转ASCII chr(96)="a" ord() ASCII转数字 ord("a")=96 isspace() 判断是否为空格 s ...
- Python学习-字符串函数操作3
字符串函数操作 isprintable():判断一个字符串中所有字符是否都是可打印字符的. 与isspace()函数很相似 如果字符串中的所有字符都是可打印的字符或字符串为空返回 True,否则返回 ...
- Python学习-字符串函数操作2
字符串函数操作 find( sub, start=None, end=None):从左到右开始查找目标子序列,找到了结束查找返回下标值,没找到返回 -1 sub:需要查找的字符串 start=None ...
- python pandas字符串函数详解(转)
pandas字符串函数详解(转)——原文连接见文章末尾 在使用pandas框架的DataFrame的过程中,如果需要处理一些字符串的特性,例如判断某列是否包含一些关键字,某列的字符长度是否小于3等等 ...
- 【python】字符串函数
1.String模块中的常量: string.digits:数字0~9 string.letters:所有字母(大小写) string.lowercase:所有小写字母 string.printabl ...
- Python之字符串函数str()
str()方法使Python将非字符串值表示为字符串: age = 23 message = "Happy" + str(age) +"rd Birthday"
- Python学习-字符串函数操作1
字符串的函数操作 capitalize():可以将字符串首字母变为大写 返回值:首字符大写后的新字符串 str = "liu" print(str.capitalize()); / ...
随机推荐
- 009-ThreadPoolExecutor运转机制详解,线程池使用1-newFixedThreadPool、newCachedThreadPool、newSingleThreadExecutor、newScheduledThreadPool
一.ThreadPoolExecutor理解 为什么要用线程池: 1.减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务. 2.可以根据系统的承受能力,调整线程池中工作线线程的数 ...
- Java-idea-常用插件-lombok
1.插件安装 打开perferences或者settings,找打plugins,选择Browse repositories...,搜索lombok,下载安装重启即可. 2.支持的注解: 2.1.@G ...
- element
<el-table-column label="地址" prop="address"> <template slot-scope=" ...
- sap gui 定义类并实现接口
1: 直接在类属性的interfaces 框输入 接口名称即可.
- Scala泛型[T]的使用
package com.dtspark.scala.basics /** * 1,scala的类和方法.函数都可以是泛型. * * 2,关于对类型边界的限定分为上边界和下边界(对类进行限制) * 上边 ...
- (转)Mysql LIMIT如何正确对其进行优化
以下的文章主要是对Mysql LIMIT简单介绍,我们大家都知道LIMIT子句一般是用来限制SELECT语句返回的实际行数.LIMIT取1个或是2个数字参数,如果给定的是2个参数,第一个指定要返回的第 ...
- 三维bfs(HUD1253胜利大逃亡)
#include <stdio.h>#include <string.h>int map[51][51][51];int v[51][51][51];int a,b,c,t11 ...
- git checkout .还可以恢复吗
说实话,希望很渺茫, 如果你在git checkout . 之前操作了git stash ,还是可以恢复的,操作如下: 最后修改文件恢复了! 但是如果你在git checkout .之前没有git ...
- js简易计算器底层运算逻辑(带撤销功能)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Eclipse + Maven 安装配置
1. 下载 http://maven.apache.org/download.cgi 2. 解压 3. 配置环境变量 MAVEN_HOME = D:\Software\apache-maven-3.5 ...