金点网络-全网资源,一网打尽
  • 网站首页
    • 金点部落
    • 小游戏
    • OpenAPI
    • 设计资产导航
    • 升级会员
  • 技能学习
    • 体育运动
    • 办公教程
    • 口才演讲
    • 小吃技术
    • 建站教程
    • 摄影教程
    • 棋牌教程
    • 网赚教程
      • 爆粉引流
      • 自媒体
      • 贴吧引流
  • 网站源码
    • 商城/淘客/交易
    • 小说/漫画/阅读
    • 影视/音乐/视频
    • 微信/微商/微擎
    • 理财/金融/货币
    • 模板/主题/插件
  • 游戏源码
    • 精品网单
    • 端游源码
    • 手游源码
    • 页游源码
  • 素材资料
    • 电子文档
    • 综合资料
    • 考研资料
    • 设计素材
    • 音频讲座
      • 人文艺术
      • 名师讲座
      • 说书小说
  • 软件工具
    • Windows软件
    • MacOS软件
    • Android软件
  • 寄售资源
    • 游戏源码
    • 网站源码
    • 软件源码
  • 公益服
登录/注册
  • 专享大神特权
立即开通开通会员抄底价

Python数据分析(中英对照)·Strings 字符串

作者 : jamin 本文共7274个字,预计阅读时间需要19分钟 发布时间: 2020-10-18 共1206人阅读

1.2.5: Strings 字符串

字符串是不可变的字符序列。
Strings are immutable sequences of characters.
在Python中,可以将字符串括在单引号、引号或三引号中。
In Python, you can enclose strings in either single quotes,in quotation marks, or in triple quotes.
让我们看一下字符串上的几个常见序列操作。
Let’s look at a couple of common sequence operations on strings.
让我先定义一个字符串。
Let me first define a string.
让我们来看看“Python”
Let’s just go with "Python."
同样,如果我想知道我的字符串有多长,我可以使用len函数。
Again, if I wanted to find out how long is my string,I can use the len function.
或者,如果我想访问该字符串的第一个或最后一个元素,我可以使用我的通用序列操作。
Or if I wanted to access say the first or the last element of that string,I can use my common generic sequence operations.
我也会做切片。
I can also do slicing.
所以我可能想从字符串的最开始开始,取前三个对象。
So I might want to start from the very beginning of the string and take the first three objects.
在这种情况下,我指定切片的起点和终点。
In that case, I specify the starting point of the slice and the end point of the slice.
所以在这种情况下,我得到字母“Pyt”
So in this case, I get the letters "Pyt."
因此Python向我返回一个新字符串。
So Python returns a new string to me.
我也可以使用负索引进行切片。
I can also do slicing using negative indices.
例如,如果我键入S,减去3,Python将给出该序列中的最后三个字符,即h、o和n。
So for example, if I type S, minus 3, Python will give me the last three characters in that sequence,in that string, which are h, o, and n.
我还可以使用字符串测试成员资格。
I can also test for memberships using the strings.
例如,假设我想问,字符y是我字符串的一部分吗?
For example, imagine I wanted to ask, is the character y part of my string?
所以我可以输入y,我可以问,y在S中吗?
So I can type in my y, and I can ask, is y in S?
答案将会是真的。
And the answer is going to be True.
如果我使用大写字母Y,答案将是错误的。
If I use capital Y, the answer is going to be False.
字符串是讨论多态性的好地方。
Strings are a good place to talk about polymorphism.
请记住,多态性意味着运算符的操作取决于它应用于的对象的类型。
Remember, polymorphism means that what an operator does depends on the type of objects it is being applied to.
这就像在数学中,你必须为数字单独定义加法,而不是矩阵或其他对象。
This is just like in mathematics, where you have to define addition separately for numbers as opposed to matrices or some other object.
在Python中,我们可以使用加号将两个数字相加。
In Python, we can add two numbers together by using the plus sign.
例如,我可以问Python,12加12是多少?
So for example, I can just ask Python, what is 12 plus 12?
我也可以把两个字符串加在一起。
I can also add two strings together.
在这种情况下,该操作称为串联,而不是加法。
In that case, the operation is not called addition, but concatenation.
让我们在这里有一个字符串,“你好。”
So let’s have one string here, "hello."
我们可以将其添加到另一个字符串中。
We can add that to another string.
实际上Python所做的是:
And actually what Python does is the following:
它接受我的第一个字符串,接受第二个字符串,并将这两个字符串连接在一起。
It takes my first string, it takes the second string,and it concatenates those two strings together.
结果是一个新字符串,其中前两个字符串已放在一起。
The result is a new string where the previous two strings have been put together.
让我们先考虑数字的乘法运算。
Let’s think about the multiplication operation first with numbers.
比如,在数学中,如果我们说3乘以5,我们真正的意思是5加5加5,也就是15。
Say, in mathematics, if we say something like 3 times 5, what we really mean is 5 plus 5 plus 5, which would be 15.
如果我们在Python中有一个字符串——我们称之为s——如果我们键入三次s,Python会将其转换为s+s+s。
If we have a string in Python– let’s called that S–and if we type something like three times S,Python will turn this into S plus S plus S.
两个数字之间的加号表示加法,而两个字符串之间的加号表示串联。
A plus sign between two numbers means addition,whereas a plus sign between two strings means concatenation.
因此,在Python中说三次S,其中S是一个字符串对象,是非常有意义的。
Therefore to say three times S, where S is a string object,makes perfect sense in Python.
让我们试试这个。
Let’s try this out.
让我们定义一个字符串对象s。让我们使用“Python”。
Let’s define a string object S. Let’s just go with "Python".
如果我键入3次S,Python将获取该字符串并与自身连接。
If I type 3 times S, Python is going to take that string and concatenate with itself.
结果就是“PythonPythonPython”,一个新字符串。
So the result is "PythonPythonPython", a new string.
在我们这里看到的示例中,我们在两个对象之间应用了加号,这两个对象要么是数字和数字,要么是字符串和字符串。
In the examples that we looked at here, we were applying a plus sign between two objects that were either a number and a number or a string and a string.
为了使多态性工作,这两个对象必须是同一类型的。
In order for polymorphism to work, these two objects have to be of the same type.
因此,虽然将数字添加到数字中、将字符串添加到字符串中是有意义的,但将字符串添加到数字中或将字符串添加到数字中则没有意义。
So while it makes sense to add a number to a number and a string to string,it does not make sense to add a string to a number or vice versa.
让我们通过一个例子来了解这一点。
Let’s look at this through an example.
让我们尝试连接一个字符串和一个数字。
Let’s try to concatenate a string and a number.
所以首先,我需要我的绳子。
So first, I need my string.
我将得到类似于“8等于”的东西——我将尝试添加一个数字,比如说8。
I’m going to have something like "eight equals"– and I’ll try to add a number,let’s say 8.
这不起作用的原因是第一个对象是字符串,第二个对象是数字。
The reason this does not work is because the first object is a string and the second object is a number.
对于我来说,要运行这一行,我首先需要做的是将数字8转换成字符串。
For me to be able to run this line, what I need to do first is to take the number 8 and turn that into a string.
我可以使用str函数显式地将数字转换为字符串。
I can explicitly turn a number to a string by using the str function.
在这个例子中,我有两个字符串,所以说一个字符串加上一个字符串是有意义的。
In this case, I have two strings, so saying a string plus a string makes sense.
结果出来了。
And the result works out.
到目前为止,我们看到的字符串操作实际上都是泛型序列操作。
The operations we have seen for string so far have really all been generic sequence operations.
除了这些操作之外,字符串还有自己的方法,使您能够操作字符串。
In addition to these operations, strings also have their own methods that enable you to manipulate strings.
为了获得一个包含所有属性的目录,我键入dir、str表示字符串,Python为我提供了一个字符串可用的不同属性的长列表。
To get a directory of all attributes, I type dir, str for strings,and Python gives me a long list of different attributes that are available for strings.
让我们看看这里有什么样的帮助。
Let’s see what type of help is available to us right here.
例如,字符串函数“replace”对我来说很有趣,所以我可以只键入strreplace,然后在末尾输入一个问号。
For example, the string function "replace" looks interesting to me,so I can just type str replace, and I can enter a question mark at the end.
这给了我一个字符串替换方法的非常简短的描述。
This gives me a very brief description of the string replace method.
让我们通过一个例子来使用replace方法。
So let’s use the replace method through an example.
我将定义一个字符串,我们称它为“name”。
I’m going to define a string,let’s call it "name".
我将在这里使用“Tina Fey”作为字符串,我们想用小写字母T替换第一个大写字母T。
I’m going to use "Tina Fey" here as my string and we’d like to replace the first capital T with a lower case t.
我调用replace方法,我想用小写的T替换大写的T。
I call the replace method,and I’d like to replace the capital T with a lower case t.
因为字符串是不可变的对象,Python实际上不会修改字符串。
Because strings are immutable objects, Python doesn’t actually modify your string.
相反,它会返回一个新字符串给您。
Instead what it does — it returns a new string to you.
如果我想保留这个新字符串,我必须将它赋给某个变量。
If I’d like to keep this new string, I have to assign it to some variable.
例如,我可以称之为“新名字”,它由原来的名字和字母T组成。
So for example, I could call this "new_name",which consists of the original name with the letter T replaced.
如果我现在看一下我原来名字的内容,你会发现T仍然是大写的。
If I now look at the content of my original name,you’ll see that the T remains capitalized.
但如果我问,我的新名字是什么?
But if I ask, what is my new name?
大写字母T现在显示为小写字母T。
The capital T now appears as a lowercase t.
让我们继续我们的例子。
Let’s continue with our example.
但这一次,让我们尝试拆分方法。
But this time, let’s try the split method.
split方法获取一个字符串并将其分解为子字符串。
The split method takes a string and breaks that down into substrings.
您必须指定要用于拆分字符串的字符。
What you have to specify is the character you would like to use for splitting the string.
我还可以使用其他字符串方法将这些字符串转换为小写或大写字母。
I can also take these strings and turn them into lowercase or uppercase letters using other string methods.
让我们找出这个对象的类型——它是一个列表。
Let’s find out the type of this object –it’s a list.
因此,我们可以询问该列表中包含多少对象。
Therefore we can ask how many objects are contained within that list.
Python告诉我们这里有两个对象。
And Python tells us we have two objects there.
因为它是一个列表,所以我们可以通过各个对象的位置来访问它们,所以写名称方括号0或名称方括号1是有意义的。
Because it’s a list, we can access individual objects by their position so it makes sense to write names square brackets 0, or names square bracket 1.
我们还可以执行以下操作:
We can also do the following:
我们可以提取列表中的第一个对象,我们可以询问该对象的类型。
We can extract the first object in that list,and we can ask what is the type of that object.
现在我们知道它是一个字符串,我们可以调用一些字符串方法来修改该名称。
Now that we know it’s a string, we can call some string methods to modify that name.
让我们用这个字符串命名方括号0,然后调用上面的方法。
So let’s take that string — names square brackets 0, and let’s call the upper method.
这会将名称“Tina”全部大写。
This turns the name "Tina" into all uppercase.
我们可以类似地获取位置1处的字符串,并使用lower方法将其转换为小写字符串。
We could similarly take, let’s say, the string at location 1,and turn that into a lowercase case string using the lower method.

Python 数据分析
本站所提供的部分资源来自于网络,版权争议与本站无关,版权归原创者所有!仅限用于学习和研究目的,不得将上述内容资源用于商业或者非法用途,否则,一切后果请用户自负。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源。如果上述内容资对您的版权或者利益造成损害,请提供相应的资质证明,我们将于3个工作日内予以删除。本站不保证所提供下载的资源的准确性、安全性和完整性,源码仅供下载学习之用!如用于商业或者非法用途,与本站无关,一切后果请用户自负!本站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。如有侵权、不妥之处,请联系站长以便删除!
金点网络-全网资源,一网打尽 » Python数据分析(中英对照)·Strings 字符串

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
是否提供免费更新服务?
持续更新,永久免费
是否经过安全检测?
安全无毒,放心食用
jamin

jamin 大神

下一篇
网络协议-组播介绍

相关推荐

Python数据分析(中英对照)·Numbers and Basic Calculations 数字和基本计算

Python数据分析(中英对照)·Numbers and Basic Calculations 数字和基本计算

Python数据分析(中英对照)·Modules and Methods 模块和方法

Python数据分析(中英对照)·Modules and Methods 模块和方法

Python数据分析(中英对照)·Introduction to NumPy Arrays NumPy 数组简介

Python数据分析(中英对照)·Introduction to NumPy Arrays NumPy 数组简介

Python数据分析(中英对照)·Using the NumPy Random Module 使用 NumPy 随机模块

Python数据分析(中英对照)·Using the NumPy Random Module 使用 NumPy 随机模块

Python数据分析(中英对照)·Introduction to Matplotlib and Pyplot-Matplotlib 和 Pyplot 介绍

Python数据分析(中英对照)·Introduction to Matplotlib and Pyplot-Matplotlib 和 Pyplot 介绍

标签云
Android Atom ExecutorService ForkJoin GM GM后台 GM授权后台 H5 Java Javascript Linux手工服务端 pipbestcom Python ReentrantLock synchronized ThreadLocal volatile Win一键即玩服务端 一键端 传奇 写作 创业 单机 后台 商业端 外网 安卓 安卓苹果双端 工具 手工端 手游 搭建教程 教程 数据分析 文案 游戏源码 端游 经典 网单 职场 自媒体 视频教程 详细搭建教程 运营后台 页游

近期文章

  • 回合手游【逍遥西游之繁华西游】最新整理单机一键既玩镜像服务端_Linux手工端_GM后台_教程
  • 最新整理精品回合制手游【天书奇谈3D混沌完整版】VM一键单机版_linux手工外网端_隐盟视频教程_授权GM后台_双端
  • 典藏修真页游【诸仙列传OL】最新整理Win系服务端_GM工具_详细外网搭建教程
  • MT3换皮MH【浮生若梦尊享挂机修复版】最新整理单机一键即玩镜像端_Linux手工服务端_安卓苹果双端_GM后台_详细搭建教程
  • 大话回合手游【最新引擎之缥缈西游渡劫版】最新整理Linux手工服务端_安卓苹果双端_管理后台_CDK后台_详细搭建教程_视频教程

分类

  • | wordpress插件 |
  • | wordpress模板 |
  • | 其它模板 |
  • | 帝国模板 |
  • | 织梦插件 |
  • | 织梦模板 |
  • A5源码
  • Android软件
  • APP引流
  • E语言
  • H5
  • LUA
  • QQ营销
  • SEO推广
  • Windows软件
  • 体育运动
  • 信息数据
  • 创业专题
  • 办公教程
  • 口才演讲
  • 名师讲座
  • 商城/淘客/交易
  • 小吃技术
  • 小说/漫画/阅读
  • 建站教程
  • 引流脚本
  • 影视/音乐/视频
  • 影视资源
  • 微信/微商/微擎
  • 微信小程序
  • 微信营销
  • 微擎模块
  • 手游源码
  • 技能学习
  • 抖音课程
  • 摄影教程
  • 棋牌教程
  • 模板/主题/插件
  • 游戏源码
  • 爆粉引流
  • 理财/金融/货币
  • 生活老师
  • 电商客
  • 电子文档
  • 电脑教程
  • 社群营销
  • 站长工具
  • 精品网单
  • 系统工具
  • 素材资料
  • 综合资料
  • 编程经验
  • 网站源码
  • 网络安全
  • 网赚教程
  • 网赚源码
  • 考研资料
  • 脚本/AI/智能
  • 自媒体
  • 英语学习
  • 营销软件
  • 设计素材
  • 说书小说
  • 贴吧引流
  • 软件工具
  • 软文营销
  • 逆向软件
  • 音频讲座
  • 页游源码

提供最优质的资源集合

立即加入 友好社区
金点网络-全网资源,一网打尽

新一代全网资源综合门户网(www.pipbest.com-金点网络)专注服务于互联网,提供各类最新最全的免费源码下载(PHP、ASP、JSP、.NET),更提供免费工具,免费源码下载,软件下载,素材下载,赚钱教程下载,交流论坛等网站运营相关的一切内容,为网友搜罗最有价值的网站源码下载与技术教程等服务!

服务目录
  • 金点OpenAPI
  • 金点云
  • 金点支付
友情链接
  • 数媒派
  • 国家电网
快速搜索

本站由Nice强力驱动

声明: 本站部分内容属于原创转载请注明出处 如有侵权行为请严格参照本站【版权声明】与我们联系,我们将在48小时内容进行处理!

本站部分内容属于原创转载请注明出处 如有侵权行为请严格参照本站【版权声明】与我们联系,我们将在48小时内容进行处理!
© 2016-2023 PipBest.Com - 金点网络 & 金点部落. All rights reserved 京ICP备2022005359号-1
  • 关注有礼
  • 签到
  • 客服
    官方QQ群 常见问题 FAQ

    在线客服

    点我联系

    直接说出您的需求!
    切记!带上资源连接与问题!

    工作时间: 9:30-21:30

  • 暗黑
    模式
  • 全屏
  • 投稿
    赚钱
  • 首页

  • 签到

  • 切换

  • 客服

金点网络-全网资源,一网打尽
  • 登录
  • 注册
or
or
忘记密码?
金点网络-全网资源,一网打尽
  • 网站首页 ►
    • 金点部落
    • 小游戏
    • OpenAPI
    • 设计资产导航
    • 升级会员
  • 技能学习 ►
    • 体育运动
    • 办公教程
    • 口才演讲
    • 小吃技术
    • 建站教程
    • 摄影教程
    • 棋牌教程
    • 网赚教程 ►
      • 爆粉引流
      • 自媒体
      • 贴吧引流
  • 网站源码 ►
    • 商城/淘客/交易
    • 小说/漫画/阅读
    • 影视/音乐/视频
    • 微信/微商/微擎
    • 理财/金融/货币
    • 模板/主题/插件
  • 游戏源码 ►
    • 精品网单
    • 端游源码
    • 手游源码
    • 页游源码
  • 素材资料 ►
    • 电子文档
    • 综合资料
    • 考研资料
    • 设计素材
    • 音频讲座 ►
      • 人文艺术
      • 名师讲座
      • 说书小说
  • 软件工具 ►
    • Windows软件
    • MacOS软件
    • Android软件
  • 寄售资源
    ►
    • 游戏源码
    • 网站源码
    • 软件源码
  • 公益服
×
u3** 刚刚下载了 爆款吸金文案训练

    全网资源·一网打尽

  • 金点出品,必属精品!
  • 发布原创内容,获取高额提成!
  • 我们与你共创美好数字生态!
  • 无特殊说明密码默认:pipbest.com