join合并,以join前的string为分隔符,将列表中的元素合并为一个新的字符串

str_1='*'.join(['Are','you','ok'])

print(str_1)

#结果Are*you*ok

分隔,split将string根据分隔符分隔成列表,也可以带参数num(分隔次数)

splitlines,按照行('\r', '\r\n', \n')分隔,可以带参数是否保留换行符('\r', '\r\n', \n'),默认为 False,不包含换行符,如果为 True,则保留换行符

print(str.split())

#结果['hello', 'world']

print(str.split('o'))

#结果['hell', ' w', 'rld']

print(str.split('o',1))

#结果['hell', ' world']

strline='hello\nword'

print(strline.splitlines(),',',strline.splitlines(True))

#结果['hello', 'word'] , ['hello\n', 'word']

字符串的开始与结束判断endwith,startswith

#3.1endwith判断以什么结尾,参数为1字符串,2开始位置,3结束位置

print(str.endswith('ld'),str.endswith('l'),str.endswith('o',2,5),str.endswith('l',2,5))

#结果True False True False

#3.2startswith判断以什么开始,同理

print(str.startswith('he'),str.startswith('e'),str.startswith('h',1,4),str.startswith('e',1,4))

#结果True False False True

字符串的isX判断

print(str.isalnum(),str.isalpha(),str.isascii(),str.isdecimal(),str.islower())

#结果False False True False True

isX说明,都是返回True或者Flase

string.isalnum() 如果 string 至少有一个字符并且所有字符都是字母或数字

string.isalpha()如果 string 至少有一个字符并且所有字符都是字母

string.isdecimal()如果 string 只包含十进制数字

string.isdigit()如果 string 只包含数字

string.islower()如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写

string.isnumeric()如果 string 中只包含数字字符

string.isspace()如果 string 中只包含空格

string.istitle()如果 string 是标题化的(见 title(),每个单词都是首字母大写)

string.isupper()如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写

字符串的大小写格式化,title标题化(每个单词首字母大写),upper全大写,lower全小写,capitalize首字母大写,swapcase翻转大小写

print(str.title(),'**',str.lower(),'**',str.upper(),'**',str.capitalize(),'**',str.swapcase())

#结果Hello World ** hello world ** HELLO WORLD ** Hello world ** HELLO WORLD

字符串的位置对齐格式化,center居中,rjust右对齐,ljust左对齐,参数1是对齐的位置,参数2对齐的字符,

特殊的对齐:zfill返回指定长度的字符串,原字符串右对齐,前面填充0

print(str.center(20),',',str.center(20,'*'))

#结果 hello world ,****hello world*****

print(str.rjust(20),',',str.rjust(20,'*'))

#结果 hello world , *********hello world

print(str.ljust(20),',',str.ljust(20,'*'))

#结果hello world , hello world*********

print(str.zfill(20))

#000000000hello world

print(str.ljust(20),',',str.ljust(20,'*'))

字符串的删除指定字符的格式化,strip去掉首尾的指定字符(默认空格),srtip是去掉右边的,lstrip是去掉左边的

str1= ' hello world '

print(str1.strip(),',',str.strip('d'))

#结果hello world , hello worl

print(str1.rstrip(),',',str.rstrip('d'))

#结果 hello world , hello worl

print(str1.lstrip(),',',str.lstrip('h'))

#结果hello world , ello world

字符串的查找,find,查找是否包含某字符,可以带开始和结束位置,找到就返回索引值,没找到就返回-1

index与find作用一致,但没找到会报异常,也可以带开始和结束位置,rfind和rindex从右向左查询,其余参数一致

print(str.find('o'),str.find('a'),str.find('o',10,20))

#结果4 -1 -1

print(str.index('o'),end=',')

try:

print(str.index('a'))

except:

print('error')

#结果4,error

print(str.rfind('o'),str.rindex('o'))

#结果7 7

字符的替换,replace替换字符(默认全部替换,可以指定次数),expandtabs可以将tab替换成空格,值为空格的个数,默认8(等于tab的空格)

print(str.replace('o','a'),',',str.replace('o','a',1))

#结果hella warld , hella world

strtab='hello\tworld'

print(strtab,',',strtab.expandtabs(1))

#结果hello world , hello world

partition() 方法用来根据指定的分隔符将字符串进行分割,像 find()和 split()的结合体,返回的元组,rpartition从右边开始

strwww='www.baidu.com'

print(strwww.partition('.'),strwww.rpartition('.'))

#结果('www', '.', 'baidu.com') ('www.baidu', '.', 'com')

format,格式化,类似%,format 函数可以接受不限个参数,位置可以不按顺序

print("{} {}".format("hello", "world") )

#结果hello world

print("{0} {1} {0}".format("hello", "world") )

#结果hello world hello

python不支持的函数string_Python字符串string常用方法和函数相关推荐

  1. C++中使用strtok函数分割字符串String

    C++中使用strtok函数分割字符串String string str; getline(cin,str); vector<string> vec; char *p = strtok(( ...

  2. C#通过函数名字符串执行相应的函数

    如果代码中函数过多,那么通过函数名字符串执行相应的函数会更加方便,也会使代码更为简单. 在C#中,通过函数名字符串执行相应的函数这项功能是在System. Reflection命名空间中实现的,使用的 ...

  3. python中str函数_python字符串str的常用函数

    1 大小写相关的函数,将字符串改成大写upper,改成小写lower,大小写切换swapcase,首字母大写capitalize,每个单词的首字母大写title,判断是否为大写isupper,判断是否 ...

  4. oracle字符串提取函数,oracle字符串分割和提取函数定义

    oracle字符串分割和提取函数定义 oracle字符串分割和提取 分割 create or replace function Get_StrArrayLength ( av_str varchar2 ...

  5. string.h包含哪些函数_Excel进行数据分析常用方法及函数汇总—【杏花开生物医药统计】...

    Excel是数据分析工作中经常使用的一种工具,经常包含着大量的原始数据,它功能十分强大,除了能录入.整理数据之外,还能进行一些常规的基础的数据分析,那么这里面就需要用到很多函数,今天就来给大家介绍一些 ...

  6. C++ 函数返回临时string调用c_str()函数的坑(VS警告:C26815 指针无关联,因为它指向已销毁的临时实例)(悬空指针 dangling pointer)

    文章目录 问题背景 20230301 问题深入解析:悬空指针 问题背景 如: ... #pragma warning(disable : 4996) #include <iostream> ...

  7. python的内置函数string_Python错误:内置函数或方法对象没有属性“StringIO”

    我只想下载一张图片.然后上传到Amazon S3.但它不起作用.在'builtin_function_or_method' object has no attribute 'StringIO' Tra ...

  8. JavaScript字符串String常用方法介绍

    JavaScript字符串在底层是一个字符串数组,比如hello字符串在底层是["h","e","l","l",&quo ...

  9. oracle数组转换字符串函数,Oracle 字符串转数组的函数

    create type char_table is table of varchar2(4000);--创建自定义类型脚本 create or replace function split_strin ...

最新文章

  1. java从入门到精髓 - 反射Constructor
  2. git push 如何同时推送至两个git仓库
  3. 刷新后 页面 保持滚动条位置
  4. 基于Pyspark和Thunder的神经图像数据分析-实验运行结果
  5. 无锁队列设计思路以及简要代码
  6. JSON.parse()解析单引号错误的问题
  7. GitHub与GitLab的区别
  8. dba 权限_DBA如何玩转PG用户、角色和权限管理?
  9. 80c51汇编语言指令格式中的非必须,求单片机答案
  10. response.setContentType(“text/html;charset=utf-8“)后依然乱码的解决方法
  11. 智能城市即将爆发,WiFi行业再迎机遇
  12. Java集合框架源码解读(4)——WeakHashMap
  13. Java int数组转List
  14. 科研论文研读工具及英文论文写作
  15. window下nginx实现图片缩放实操
  16. Springboot定时任务【多线程处理】
  17. 计算机电源接口在哪,电脑电源接口定义图解
  18. 怎么把线稿提取出来_ps怎么把彩色图片提取线稿出来,就黑白线稿的那种 要详细!...
  19. DBCHART的使用
  20. 文读懂安防视频监控系统中H.265、SVAC、GB/T28181、ONVIF、PSIA的区别。

热门文章

  1. Component Xxx is not part of any NgModule or the module has not been imported into your module.
  2. 中奖人js滚动效果_jQuery实现中奖播报功能(让文本滚动起来) 简单设置数值即可...
  3. MS2109/HDMI转USB2.0高清视频采集
  4. iphone7plus计算机,iPhone 7 Plus和iPhone 8 Plus的区别-太平洋电脑网
  5. Knockoutjs官网翻译系列(二) Observable 数组
  6. 前锋linux试题,大学篮球考试题库.doc
  7. 房价这么高,为什么租金却高不起来?
  8. 最强大脑 奇虎360 2017校园招聘笔试题
  9. android音乐播放器横评,14款Android平台音乐播放器横评
  10. 如何设计出别具一格的全息投影餐厅