未登录,请登录后再发表信息
最新评论 (0)
播放视频

9/44 字符串的概念

String Concepts | Python for Beginners [9 of 44]

使用字符串
Let’s get in and take a look at
让我们开始学习
probably one of the most common things
编程中可能最常用到的
that you’ll be doing in programming,
一样事物
and that is working with strings.
那就是使用字符串
Now, when it comes to strings and actually just variables,
好 字符串实际上是变量
In Python, it’s relatively straightforward to take a string
在Python里创建字符串并将其储存在变量中
and store it inside of a variable.
相对比较简单
Now as a real quick aside,
顺便提一下
if you’re not already familiar with variables,
如果你对变量还不太熟悉
variables windup acting as placeholders
变量在代码中
inside of your code for some values.
充当某些值的占位符
So in my case,
在此示例中
first_name is going to wind up being “Christopher”.
first_name赋值为“Christopher”
Now, one of the things that will make Python unique is the fact
Python独特的一点在于
that you don’t have to use
你不需要使用
any form of a keyword or otherwise to declare a variable.
任何形式的关键字或其他语句来声明变量
You just simply give it a name,
你只要简单起个名字
set it to some value,
给它赋一个值
voila, you’ve created a variable that’s all there is to it.
看吧 你已经创建好一个完整的变量了
You’ll also notice my string literal over here on the right-hand side,
你会发现对于在右边的字符串
and you’ll notice that I’m using single quotes here.
我使用的是单引号
Again, you can use single quotes or double quotes,
强调一下 你使用单引号或双引号都可以
doesn’t matter which, but you want to be consistent.
哪个都行 但你要保持一致
For me personally, I really like using single quotes.
我自己比较喜欢用单引号
I just think it reads a little bit better
我只是认为它看起来
than having double-quotes.
比双引号好一点
Maybe I don’t even have a good reason as to why.
也许我也说不出一个更好的理由
I just think it looks better with
我只是认为单引号看起来
single quotes rather than double-quotes.
比双引号好一点
If you want to use double-quotes,
如果你想用双引号
you’re not going to hurt my feelings at all.
我一点也不介意
It’s your code.
因为那是你的代码
Just simply again be consistent.
只是简单强调一下 要保持一致
Now if you want to take two strings and combine them together,
如果你想把两个字符串连在一起
you can do that by just using the plus operator.
用加号运算符就可以了
So you’ll notice in my code
可以看到在我的代码中
that I’ve got my first name and my last name,
我有first_name和last_name
and then you’ll notice that I’m going ahead
我们写完这个后继续
and I’m calling Print,
然后我调用了print
and I’m combining that first name and last name together
你会发现first_name和last_name之间
just by using that little plus sign
我只用了一个小小的加号
that you see right there.
就把它们连起来了
Let me just grab my pen again
我现在用下钢笔工具
so that way I can circle it.
这样我可以把它们圈出来
That little plus sign will bring that together.
这个小加号可以把它们连起来
Now that works with both variables, as well as with string literals.
这对于变量和字符串都适用
So if I want to bring together
所以如果我想
a string literal of “Hello” and a couple of spaces
在字符串里加上字符串 ‘Hello’
along with my strings,
还有一些空格
then I can also do that just by using
我也同样可以使用
that exact same plus operator.
这个加号运算符
One last little side-note on our variables.
关于变量顺便再说一点
You’ll notice that we’re using
你会发现我们用到
a word, an underscore, and then a word.
一个单词 一个下划线再加一个单词
When you’re creating variable names,
当你给变量起名字的时候
you want to make sure that they’re nice and clear,
你要确保名字简单易懂
and if they are going to need multiple words to make it clear,
如果需要几个单词才能解释清楚的话
convention in Python is always lowercase,
在Python中 一般是用像这样的
and with that underscore that you see there.
小写字母和下划线
Now, if you want to modify a string,
现在 你如果想修改字符串
maybe convert everything into uppercase letters, lowercase letters,
比如将所有字母转成大写或小写
capitalize just the first word,
首字母大写
or potentially count all of the instances of a particular string.
或者是获得某个特定字符串的数量
In this case the letter a,
比如这里的字母a
you can do that by using upper, lower,
你可以分别使用upper lower
capitalize, encount, respectively, and down below
capitalize count 然后运行
you’ll see the results of doing each one of those.
你会看到每一条语句的运行结果
Then finally, if you want to bring all of that together,
最后 如果你想把所有的连在一起
you can still do that string concatenation
你可以按照我们之前说的方法
just the way that we’ve seen previously,
完成字符串连接
and even bring in values from the outside world.
甚至从其他地方加一些值进去
So in this little code example here,
所以在这个代码示例中
what you’re going to notice is
你能看到的是
we’re going to read in the first name from the input window.
我们首先会读取输入框中的first_name
So whatever it is that the user types in will bring that in.
无论用户输入什么都会被存储
We’ll do the exact same thing
last_name也是一样
with the last_name and print that out,
然后把它们输出
and then you’ll notice that
接下来你会看到
we can properly capitalize everything
我们使用capitalize函数
by utilizing that capitalize function that you see there,
使所有单词首字母大写
and you’ll notice the results,
可以看到结果中
that if I type in Christopher in all uppercase,
我输入的Christopher全是大写
Harrison in all uppercase,
Harrison也全是大写
and then I wind up calling capitalize,
但紧接着我调用了capitalize
it will just capitalize that first letter.
就只有首字母大写了
So that’s how you can get in
这就是怎么在Python里
and start working with some neat little strings inside of Python.
一些处理简单字符串的方法了
Now what I want to do is go off, do a little bit of a demo,
接下来我想演示一些例子
and then we’ll actually come back
然后我们回过头来
and take a look at one last advanced configuration that you can do with strings.
学习最后一种跟字符串相关的高级配置

发表评论

译制信息
视频概述

本视频介绍了编程中最常用的一个概念——字符串

听录译者

收集自网络

翻译译者

eyas闯江湖

审核员

审核员1024

视频来源

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

相关推荐