你知道字符串是什么意思吗 常用的字符串有哪些呢

来源:三秦科技网

字符串(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 )

user _ input=' $ { error._ _ init _._ _ globals _ [ secret ] } ' template (user _ input ).substitute )。 如果格式化字符串是用户提供的,请使用模板字符串。 为了避免安全问题,如果使用的是python 3.6,请使用字符串插值。 如果使用的是python 3.6之前的版本,请使用第二种,即新的字符串格式化方法

标签: 编程语言 数据类型 二进制数字串 连续序列

推荐

财富更多》

动态更多》

热点