ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

12/44 字符串格式化示例 – 译学馆
未登录,请登录后再发表信息
最新评论 (0)
播放视频

12/44 字符串格式化示例

Demo: Formatting Strings | Python for Beginners [12 of 44]

《字符串格式化代码》
Let’s see how we can do that string format.
让我们来看看如何将字符串格式化
So I’m going to start off by doing first_name equals Christopher
那么首先 和之前一样 我设first_name为Christopher
and last_name equals Harrison, just like before.
last_name为Harrison
Again, what we could do is,
然后我们可以这样做
we could just simply output equals
简单地把output的值设为
our ‘Hello’ and our little plus and our first_name,
‘Hello’ 加号 first_name
a little plus, a space,
加号 空格
and then hello and—oops—and then our last_name at the end there.
然后是’Hello’ 嘴瓢了 最后加上last_name
There we go, and then I say print and our output,
另起一行 输入print output
and that will of course give us, our…
现在我们就能得到…
if I run this correctly, there we go.
运行正确的话 结果会是这样
That will give us our little”Hello, Christopher.”
结果为 Hello, Christopher
Now, I’m going to do the exact same thing multiple times
现在 我将多次重复相同的步骤
and we should just get the exact same output.
那我们应该得到相同的结果
My goal here is really just
我的目的只是
to show the different ways that we can do this.
教大家一些不同的方法
So let me comment that out, and now,
现在我把这条代码改为注释
let’s do this again by using the little placeholders.
这次我们用一些小的占位符达到效果
So we’ll go ahead and say, “Hello”,
我们还是以Hello开头
and then we’ll just do two curlies,
然后输入一对大括号
and then two curlies,
再输入一对大括号
and now I’ll say format.
现在输入format
Again, using tab to give me that completion there,
之后按下tab键让它自动补全
and I’ll say first_name and last_name, there we go.
再输入first_name和last_name
Let’s go ahead and save that, rerun it,
我们继续 保存 再次运行程序
once again, the exact same output, “Hello, Christopher Harrison”.
依然输出了相同结果“Hello, Christopher Harrison”
If I want to maybe be specific about it,
如果我想要输入得更具体一点
so let’s go in and say 1 and 0, and go ahead and run it.
可以在2个大括号内分别输入1和0 然后运行
Now, you’re going to notice we’ll get the exact same output,
你会注意到我们依然得到相同的结果
but maybe if I wanted to reverse that last_name and first_name,
但如果我想将first_name和last_name的值对调
maybe I want to put a comma between them,
可能我还想在它们中间加一个逗号
so I’ll go in and I do 1 and then 0,
那么我可以将1和0反过来
put a comma in the middle.
并在2个大括号中间输入一个逗号
Now, what you’re going to notice when I run this is that,
现在 运行后你会看到
it went ahead and gave me Hello Harrison,Christopher,
结果是“Hello Harrison, Christopher”
without me having to change the order of the parameters,
我无需改变参数的顺序
which is another advantage to going in and specifying the numbers.
这就是指定数字的另一个好处了
Now again, my preferred mechanism is just to use that cool little format string.
重申一次 我最喜欢直接用这个很酷的小格式串
So I’m going to say output equals and then we’ll go F,
我会输入output=f
and now I’ll say,
之后输入
Hello, comma, curly, and then we’ll say first_name, space, curly, last_name, and
Hello, {first_name} {last_name}
you’ll notice the intellisense actually
在大括号中 你会注意到
comes right along for the right there,
右侧会有智能提示
suggesting first_name and last_name for me,
提示我输入first_name和last_name
and now I’ll go ahead and hit “Save”,
我现在点击“保存”
and I rerun that,
然后再运行一次
and now we’re back to that Hello, Christopher Harrison.
我们又可以看到“Hello, Christopher Harrison”的结果
So the big takeaway that I want you to
所以从这些例子中
get out of all of those examples is that
我最想让你们知道的是
there are multiple ways that you can do
你可以用多种方式来得到
the exact same types of string concatenation.
同一个字符串结构
It’s really up to you to decide which way it is that you prefer.
实际用哪种取决于你更喜欢哪种
For me, like I said previously,
就像我之前说过的 对我来说
I really like that last string format
我真的很喜欢最后那种字符串格式化的方式
with the little f at the very beginning there.
在最开始输入一个小写的f
That to me reads the best,
这个对我来讲是最好的方法
and you’ll also notice that there’s a lot of
你以后也会注意到
other programming languages
有许多其他的程序语言
that have a very similar construct such as in say
它们都有非常相似的结构 比如说
JavaScript or in C sharp, for example.
JavaScript 或是C#
I also really like the fact that it is self-documenting,
我也很喜欢自文档化的编程语言
because I can very clearly see, oh,
因为我可以很清楚地知道
that’s where first_name is going to
first_name会在这里
be and that’s where last_name is going to be.
last_name会在那儿
That’s how we can work with strings inside of Python.
Python中的字符串编程语言就是这样操作的

发表评论

译制信息
视频概述

字符串格式化示例:用不同的方式输出同一个字符串

听录译者

收集自网络

翻译译者

审核员

审核员CH

视频来源

https://www.youtube.com/watch?v=E850-MF22P0

相关推荐