博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python3 字符串操作
阅读量:4228 次
发布时间:2019-05-26

本文共 3958 字,大约阅读时间需要 13 分钟。

字符串中嵌入变量

# Python3>>> name1 = "Joe">>> name2 = "Mary">>> print(f"你好 {name1}, {name2} 在哪?")你好 Joe, Mary 在哪?

 字符串内建方法

# Python3>>> myStr = str("THIS IS TEST")>>> myStr'THIS IS TEST'>>> foo = myStr.lower()>>> foo'this is test'

 字符串自我复制

# Python3>>> aa = "hello world">>> aa * 3'hello worldhello worldhello world'

字符串拼接

# Python3>>> "hello" + " world"'hello world'

字符串比较

# Python3>>> "hello" < "world"True>>> 0 if "hello" == "world" else (-1 if "hello" < "world" else 1)-1>>> "hello" == "world"False
# Ruby[13] pry(main)> "hello" < "world"=> true[14] pry(main)> "hello" <=> "world"=> -1[15] pry(main)> "hello" == "world"=> false

正则表达式匹配

# Python3>>> import re>>> re.search("ll", "hello world")<_sre.SRE_Match object; span=(2, 4), match='ll'>>>> re.search("o.*o", "hello world")<_sre.SRE_Match object; span=(4, 8), match='o wo'>

字符串子串

# Python3>>> "hello world"[0:5]'hello'>>> "hello world"[0:-6]'hello'

str.capitalize()

# Python3>>> aa = "hello world">>> aa.capitalize()'Hello world'

str.casefold()

和 lower() 比较像,casefold 支持很多不同种类的语言。比如说 β。str.lower() 只能显示出原形而 casefold 则能显示他的小写。

# Python3>>> "Hello woRld ß".casefold()'hello world ss'>>> "Hello woRld .ß".lower()'hello world ß'

str.center(width, fillchar)

# Python3>>> aa = "hello world">>> aa.center(20)'    hello world     '>>> aa.center(20, '*')'****hello world*****'

str.count(substr) 

# Python3>>> "hello world".count('o')2

str.endwiths(suffix)

# Python3>>> aa = "hello world">>> aa.endswith("ld")True>>> aa.endswith("l")False

str.index(substr)

跟 find() 方法一样,只不过如果 substr 不在字符串中会报一个异常。

# Python3>>> aa = "hello world">>> aa.find('o')4>>> aa.index('o')4>>> aa.index('ol')Traceback (most recent call last):  File "
", line 1, in
ValueError: substring not found>>> aa.find('ol')-1

str.isalnum()

# Python3>>> aa = "hello world">>> aa.isalnum()False>>> aa = "helloworld5">>> aa.isalnum()True

str.isalpha()

# Python3>>> aa = "helloworld">>> aa.isalpha()True>>> aa = "hello world">>> aa.isalpha()False

str.isdigit()

str.isdigit() 检测字符串是否只包含数字(即不接受其他一切非 [0-9] 元素)。

# Python3>>> aa = "hello world">>> aa.isdigit()False>>> aa = "12345">>> aa.isdigit()True

str.isnumeric()

# Python3>>> "12345Ⅲ".isnumeric()True>>> "12345.67".isnumeric()False

str.islower()

# Python3>>> "hello world".islower()True>>> "Hello world".islower()False

str.isupper()

# Python3>>> "hello world".isupper()False>>> "HELLO WORLD".isupper()True

str.isspace()

# Python3>>> "hello world".isspace()False>>> "       ".isspace()True

str.istitle()

# Python3>>> "hello world".istitle()False>>> "Hello World".istitle()True

str.isdecimal()

# Python3>>> "2345".isdecimal()True>>> "0aff".isdecimal()False

str.join(seq)

# Python3>>> aa = ['h', 'e', 'l', 'l', 'o']>>> "".join(aa)'hello'>>> "-".join(aa)'h-e-l-l-o'

str.ljust(20)

# Python3>>> "hello world".ljust(20)'hello world         '>>> "hello world".ljust(20, '-')'hello world---------'

str.lstrip()

# Python3>>> "  hello world".lstrip()'hello world'

str.lower()

# Python3>>> "Hello World".lower()'hello world'

str.replace(old, new [, max])

# Python3>>> "hello world".replace("world", "hello")'hello hello'

str.rjust(20)

# Python3>>> "hello world".rjust(20)'         hello world'>>> "hello world".rjust(20, '-')'---------hello world'

str.rstrip()

# Python3>>> "hello world  ".rstrip()'hello world'

str.strip()

# Python3>>> "  hello world  ".strip()'hello world'

str.split(seperator)

# Python3>>> "hello world".split()['hello', 'world']>>> "hello world".split('l')['he', '', 'o wor', 'd']>>> "hello world".split('l', 2)['he', '', 'o world']

str.startswith(prefix)

# Python3>>> "hello world".startswith("hello")True

str.swapcase()

# Python3>>> "Hello World".swapcase()'hELLO wORLD'

str.title()

# Python3>>> "hello world".title()'Hello World'

str.upper()

# Python3>>> "Hello World".upper()'HELLO WORLD'

str.zfill(num)

# Python3>>> "10110".zfill(8)'00010110'

len(str)

# Python3>>> len("hello world")11

max(str)

# Python3>>> max("hello world")'w'

min(str)

# Python3>>> min("hello world")' '

转载地址:http://ajjqi.baihongyu.com/

你可能感兴趣的文章
内存操作越界略述
查看>>
消息与消息队列
查看>>
关于#include "stdafx.h"
查看>>
VC下线程同步的三种方法(互斥、事件、临界区)/(转)
查看>>
非常好的一篇U-BOOT的文章--转载
查看>>
【设计模式】学习之创建型 单例模式
查看>>
【设计模式】学习之创建型 原型模式
查看>>
【设计模式】学习之结构型 适配器模式-装饰器模式-代理模式
查看>>
Maven+Eclipse+Tomcat+Spring MVC web 请求 404 问题总结及noHandlerFound
查看>>
SpringMVC API缓存 LastModified的实现总结
查看>>
406 Not Acceptable 415 Unsupported Media Type Spring MVC consumes与produces
查看>>
MyBatis 高级映射与懒加载
查看>>
HCIP-H12-222练习题
查看>>
点到点IPSec VPN的配置
查看>>
MySQL InnoDB何时更新表的索引统计信息
查看>>
MTU 设置错误导致防火墙或者路由器断网
查看>>
子网划分详解与子网划分实例
查看>>
游戏通讯技术:帧同步技术
查看>>
防火墙技术指标---并发连接数/吞吐量
查看>>
V100服务器和T4服务器的性能指标
查看>>