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

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

11/44 字符串格式化

Formatting Strings | Python for Beginners [11 of 44]

>> So previously, we saw how we could take a couple of strings
在前面的学习中 我们知道了如何将多个字符串
and combine them together
用加号运算符
by using that literal plus operator.
把它们连起来
Now, we’ve already seen this slide and
现在 我们已经看过这张幻灯片了
if you take a look at the code in particular that fourth line,
如果你看一下这个代码 尤其是第四行
what you’re going to notice there is there’s a lot going on,
你就会注意到 这里做了很多事
that I’ve got that little plus right there in the middle,
我在这中间用了一个小加号
with my string literal,
连接这个字符串常量
and then I’ve got another plus here,
然后这里有另一个加号
and another string literal,
和另一个字符串常量
and another plus here,
这里有又一个加号
and another string literal.
又一个字符串常量
You know, this could get unwieldly pretty quickly,
这样表达式很快就会变得很笨重
and this is not even
这甚至还没有考虑到
taking into account that we might want to be calling capitalize,
我们可能想调用首字母大写函数
or upper, or lower,
全部字母大写或全部字母小写函数
or some of those other helper functions that we might have,
或者我们的一些其它辅助函数
and our codes are just going to
我们的代码只会变得
keep getting longer, and longer, and longer.
越来越长 越来越长 越来越长
So let’s try and simplify this a little bit.
所以让我们试着把这简化一点点
This is where our format strings come into play.
这就是我们的格式化字符串发挥作用的地方
Now, what we can do is that little output that we see up at the top
现在 我们来看到最上面一行的output变量
where again we’re calling
这里我们还是使用了加号
that plus sign that we’ve already seen,
这我们已经讲过了
or we can streamline this by using placeholders.
或者 我们可以使用占位符来简化掉它
Now, each one of these outputs that you see up here on the slide
现在 你看到的幻灯片上的每一个output变量
is going to give us the exact same string.
都会给出完全相同的字符串
We’re just going to do it slightly differently.
我们只是用略微不同的方式来做这件事
So the first one,
那么第一种方法
what we’re doing here is we’re going in and
我们在这里要做的是
we’re putting in place holders with those curly braces.
在里面用这些花括号占位
Now, the way that this works is it’s going to be based on
那么 这种占位的工作方式
the order in which we specify the parameters.
会基于我们所指定的参数顺序
So that first one there,
所以第一个占位在这
that’s going to be first_name in my case,
在我的示例里它将是first_name变量
and that second one,
然后第二个占位
that is going to be last_name.
将是last_name变量
Now, if we wanted to specify it,
现在 如果我们想指定占位符对应的参数
what we can do instead
我们可以换一种方式
is we can use the zero, and the one
就是可以用0和1
which then allows us to specify
来让我们分别指出
the first and the second respectively.
第一和第二个占位符指向的字符串变量
Remember, that counting will start with zero,
记住 计数是从0开始的
so that zero is going to be the first item,
所以0将是第一项
and the one is going to be the second item.
1将是第二项
Now, in my example up here,
现在 在我这里的示例中
it’s not going to make a difference,
第二种方法不会带来什么改变
that both of them are the exact same.
两种方法的结果是完全相同的
But if I need to potentially reuse
但是如果我可能需要在其它地方
the exact same string somewhere else
复用完全相同的字符串
or maybe I just want to document it,
或者我只是想记录下
show hey, this is going to be the first,
表明 嘿 这将是第一个字符串
this is going to be for the second,
这将是第二个字符串
this is going to be for the third,
这将是第三个字符串
then I could go ahead and put in that zero, the one,
那么我就可以直接在花括号中插入0 1
and then maybe a two later on as well.
然后也许再插入一个2
Now, the last example that I want to highlight here,
好 在最后一个示例中我想高亮这里
and I want to make sure that I point out the fact,
我必须要指出这一点
that was not what I wanted,
不是用这个标记
I wanted that, there we go.
我要用这个 高亮它
I want to make sure that I point out the fact that
我要明确指出的一点是
this is only available in Python 3.
这个方法只在Python 3中可用
So if you’re doing anything that needs to run in Python 2,
因此 如果你的工作需要在Python 2下运行
this last example is not going to work,
最后一个示例将无法运行
but it will work in Python 3.
但它可以在Python 3中运行
That is where I put in f right at the very beginning,
这里我在开头输入了一个f
f being for format,
f表示格式化
and now what I’m able to do and I love this functionality,
那么现在我就可以用这种方式 我喜欢这一点
is I’m able to now just use my variable names
我现在可以直接将我的变量名
right in line with everything else.
嵌入到其它部分代码里
This is my preferred method
每当我做字符串连接时
whenever I’m doing string concatenation,
这是我的首选方法
because it’s nice, it’s clear, it’s self-documenting,
因为它友好 清晰 自文档化
you always want your code to be self-documenting,
你总是希望你的代码是自文档化的
and when somebody comes back
当有人回来看代码
assuming that they understand the little f at the beginning,
假设他们理解开头的小f的话
it’s very easy for them to go,
他们读代码就很容易
“Oh, that’s going to be my first name,
“噢 那将是我的名
and that is going to be my last name.”
那将是我的姓”
Let’s see how all of this works
下一个小视频就让我们看看
inside of code in our next little video.
所有这些是如何在代码中工作的吧

发表评论

译制信息
视频概述

Python语言里进行字符串格式化操作的几种方法

听录译者

收集自网络

翻译译者

冯新怡

审核员

审核员AL

视频来源

https://www.youtube.com/watch?v=bQQqxysLIGE

相关推荐