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

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

10/44 字符串示例

Demo: Strings | Python for Beginners [10 of 44]

Strings Code
字符串代码
So let’s take a look
我们来看一下
at some of those neat little string functions that we saw previously.
一些之前看过的简洁的字符串函数
So I’m going to start off by just simply typing in
首先呢我从简单的语句开始
first_name equals Christopher,
first_name = ‘Christopher’
we’ll just skip that stored,
我们跳过这步存储
and then last_name, equals Harrison.
然后是 ‘last_name = Harrison’
Perfect. Now, like we have already seen,,
好极了 现在 正如我们所见
we can concatenate things together by using that plus sign.
我们可以通过使用加号将字符串连接起来
So if I say “print” and I say “first_name”,
因此 如果我输入print以及first_name
and by the way,
哦对了
a little IntelliSense Beta functionality for you.
介绍一个测试版功能IntelliSense
If you see the word that you want already highlighted,
如果看到你想输入的词已经高亮显示了
all that you need to do is hit “Tab”
你需要做的就是按下Tab键
and that will complete it out for you.
编辑器将会自动为你补全这个词
It’ll save you a little bit of typing.
这将帮你节省一点码字的时间
So if I say first_name plus last_ name,
因此如果我输入 first_name + last_name
save this,
保存一下
and then I go ahead and run this.
然后继续并运行
Oops, there we go.
哦写错了 好了
Make sure we make, right spot.
确保是正确的路径
You’ll notice that it prints out Christopher Harrison.
你会看到它输出的是 Christopher Harrison
Now, if I update this,
现在 我更新一下
let’s go ahead and put in our print,
然后放入print语句中
and then we’ll say ‘Hello’
然后我们希望输出 Hello
first name, a space, and then our last name.
然后是名字 一个空格 以及姓氏
And again I’m using that Tab to help me out with the auto-complete.
我再次使用了Tab键来自动补全
Now, what you’re going to notice is that it gives us
现在你会发现 它会输出
that “Hello, Christopher Harrison” with the spaces inside out there.
Hello, Christopher Harrison 以及里面的空格字符
Now, if instead of printing it out like that,
如果我不想这样输出
maybe I wanted to go ahead and bring that in from the user,
也许我想让使用者来填入这些信息
and let me just comment out the code
我来把这些代码注释掉
so that way it will still be in existence there.
将其暂时保存在此
I can go ahead and say first_name equals
我可以继续这样写 first_name = input
and we’ll say “Please enter your first name.”
在里面写 Please enter your first name:
And then actually before you do this,
实际上 在这样做之前
I want you to notice right here,
希望你能注意下这里
that little last name and those little green squiggles,
这个last_name以及它下面绿色的小波浪线
yes that is the technical term,
这是个专业术语
underneath there, that’s Visual Studio Code letting me know hey,
下面这个东西是Visual Studio Code试图告诉我
there’s something not quite right here.
你这里代码敲得有点问题哦
If you ever see that,
如果你见过这个
you want to pause,
你需要停下来
take a look at your code,
检查下你的代码
and see if there’s potentially a mistake.
看看是不是哪里写错了
In my case, the mistake is that I haven’t declared last_name yet.
我这里的错误是 我还没有定义last_name这个变量
Remember, we had it up here,
记住 我们在前面这里写了
but we’ve commented it.
但又注释掉了
Remember, what Susan taught us earlier,
记住Susan之前教过我们的
that when you comment out a line of code,
如果你注释掉了一行代码
that’s now not going to run it any longer.
那程序就无法再继续运行下去了
So let me go ahead and enter in our last name here.
所以我再在这里加入last_name变量
It will say,
这里写
“Please enter your last name:”
Please enter your last name:
Now, let’s go ahead and run this and we’ll see what happens.
现在我们再来运行一下程序 看看会发生什么
So it asks me “Please enter my first name”. Christopher.
它这里说Please enter my first name 输入Christopher
“Please enter my last name.” Harrison.
Please enter my last name 再输入Harrison
Hit “Enter” and now it tells me “Hello Christopher Harrison”.
按下Enter键 它会输出Hello Christopher Harrison
If we want to go ahead and capitalize everything,
如果我们想进一步把所有单词首字母都变为大写
we can go ahead and say capitalize and capitalize.
我们可以在这里加入capitalize函数 然后这里也是
Again, we’ll save that.
再次保存一下
And now, let’s come back and re-run our code.
然后回过头来 再次运行程序
What’s my first name? I’m going to ”shout“ it.
我的名字?我要”大声喊”出来
CHRISTOPHER.
输入CHRISTOPHER
What’s my last name?
我的姓氏?输入HARRISON
You’ll notice the results here,
你会看到这里的结果
that we wind up getting that
我们最后得到的
Christopher Harrison with the
却是Christopher Harrison
just the capital C and the capital H,
只有首字母C和H是大写的
because once again, we’ve used that capitalize from before.
因为我们再次用到了前面提到的首字母大写函数capitalize
Now, if we want to do things like lower, and upper, and so forth,
如果我们还想将字符全都小写或者大写 等等
all of those are available to us inside of Python.
我们都可以在Python中找到这些函数
Now, if you want to get in and see some of
现在如果你想继续深入并
the other things that you could potentially do with Strings,
了解一些可用用得到的字符串有关的知识
you can take a look at the docs inside of Python.
你可以查阅Python的内部文档
Those are some of the more common ones
你会在其中发现一些
that you’ll find yourself reaching for
常见的函数
when you get in and start working with Strings inside of Python.
而这一切都从与Python中的字符串认真打交道开始

发表评论

译制信息
视频概述

Python有着强大的字符串功能,让我们先从入门开始

听录译者

收集自网络

翻译译者

伽卡

审核员

审核员1024

视频来源

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

相关推荐