你知道字符串是什么意思吗 常用的字符串有哪些呢
字符串(String)是由数字、字母、下划线组成的一串字符。一般记为 s=“a1a2···an”(n>=0)。它是编程语言中表示文本的数据类型。在程序设计中,字符串(string)为符号或数值的一个连续序列,如符号串(一串字符)或二进制数字串(一串二进制数字)。
通常以串的整体作为操作对象,如:在串中查找某个子串、求取一个子串、在串的某个位置上插入一个子串以及删除一个子串等。两个字符串相等的充要条件是:长度相等,并且各个对应位置上的字符都相等。设p、q是两个串,求q在p中首次出现的位置的运算叫做模式匹配。串的两种最基本的存储方式是顺序存储方式和链接存储方式。
常用的字符串格式化方法有哪些
文章目录1 .老式字符串格式2 .新型字符串格式3 .字符串插值法(python 3.6 ) —— f-string4. Template Strings我应该选择哪种字符串格式方法?
参考:
《Python Tricks: A Buffet of Awesome Python Features》
1 .过时的字符串格式errno=50159747054 name=' bob ' ' hello,%s' % name'Hello,Bob' '%x' % errno #整数转换为十六进制数字,字符串' badc 0f
如果对一个字符串执行多个替换,则“早期版本”字符串的格式语法将略有更改。 % -运算符只接受一个参数,因此必须将右侧封装在一个元组中
' Hey %s,there is a0x%x error!' %(name,errno ) ' Hey Bob,there is a0xbadc0ffee error!' 将映射传递给% -运算符时,也可以在格式字符串中用名称引用变量替换
' hey%(name ) s,thereisa0x% ) Errno ) x error!' % {. 'name': name,' errno': errno }'Hey Bob,there is a0xbadc0ffee error!' 参考:
打印样式字符串格式
2 .新的字符串格式使用格式设置字符串格式
' Hello,{}'.format(name ) Hello,bob ) format支持将变量赋值到指定位置
' Hey {name},there is a0x{errno:x} error!' . format(name=name,errno=errno ) Hey Bob,there is a0xbadc0ffee error! '在格式中,即使交换了错误和名称的位置,仍然可以输出正确的结果
' Hey {name},there is a0x{errno:x} error!' . format(Errno=Errno,name=name ) ) Hey Bob,there is a0xbadc0ffee error! ' errno后面的:x表示输出十六进制
' int: {0:d}; hex: {0:x}; oct: {0:o}; GDDWN:{0:b}.format(42 ) int: 42; hex: 2a; oct: 52; gddwn: 101010' # with0x,0o,or0bas prefix : ' int : { 0: d }; hex: {0:#x}; oct: {0:#o}; GDDWN:{0:#b}.format(42 ) int: 42; hex:0x2a; oct: 0o52; gddwn: 0b101010 '参考:
str.format
字符串格式
格式字符串同步
3 .为字符串插值法(python 3.6 ) —— f-string python3.6添加了一种格式化字符串的新方法。 此方法允许在字符串常量中嵌入python表达式。
f'Hello,{name}!' “你好,鲍勃! ”这个方法可以嵌入python表达式,所以也可以用于内联运算。
a=5b=10f ' fiveplustenis { ab } and not {2* (ab ) }.' fiveplustenis 15 and not 30.' defgreet (name,question ) 3360 how ' . greet('Bob ',' going ' ) ' Hello,Bob! How's it going?' 那么这个函数怎么做呢? 实际上,f这个字符串执行了以下操作
defgreet(name,question ) :return ) ) Hello,' name )! How's it ' question '? ')要在f-字符串中输出其他二进制数字(如十六进制数字),请在:后加上#x。 官网上写着:#0x,打印后效果相同。
f'Hey {name},there's a {errno:#x} error! ' ' Hey Bob,there's a0xbadc0ffee error!' f'Hey {name},there's a {errno:#0x} error! ' ' Hey Bob,there's a0xbadc0ffee error!' 参考:
格式化字符串写入
4.templatestringstemplatestrings,简单。
fromstringimporttemplatet=template (' hey,$name!' (t.substitute(name=name )、Hey、Bob! '需要的是从python内置的string库读取Template,定义字符串,将该字符串放入Template () ),然后执行substitute ) )方法。
templ_string='Hey $name,there is a $error error!' template(templ_string ).substitute ) name=name,error=hex(errno ) ) Hey Bob,there is a0xbadc0ffee error! 应用' Template Strings的最好示例是当wrdy处理用户生成的字符串时。
例如,用户可以通过设置字符串的格式来获得程序中的任何变量。 这意味着,如果恶意用户可以提供格式化字符串,则还可以泄露密钥和其他敏感信息。
themorecomplexformattingmini -语言语言软件转换器格式-
tingtechniquesmightintroducesecurityvulnerabilitiestoyourpro -
grams. For example,it’spossibleforformatstringstoaccessarbitrary
在your program中启用变量。
That means,ifamalicioususercansupplyaformatstringtheycan
alsopotentiallyleaksecretkeysandothersensibleinformation!
here’sasimpleproofofconceptofhowthisattackmightbeused :
secret=' this-is-a-secret ' classerror 3360 . def _ init _ (self ) : passerr=error (_ user _ inpuinpuing uh-oh . user _ input.format (error=err ) this-is-a-secret )
免责声明: 本网均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,本网不承担此类稿件侵权行为的连带责任。如果发现有涉嫌抄袭的内容 欢迎发送邮件至1063780331@qq.com 举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。转载请保留链接: https://www.tbxxjz.com/znsd/53081.html