初识 Python:Hello World 和字符串操作 | Linux 中国

初识 Python:Hello World 和字符串操作 | Linux 中国_第1张图片本文中的代码和视频可以在我的 GitHub 上找到。-- Michael

有用的原文链接请访问文末的“ 原文链接”获得可点击的文内链接、全尺寸原图和相关文章。

致谢 编译自 | https://www.codementor.io/mgalarny/python-hello-world-and-string-manipulation-gdgwd8ymp 
 作者 | Michael
 译者 | geekpi ? ? 共计翻译:677 篇 贡献时间:1608 天

开始之前,说一下本文中的代码[1]视频[2]可以在我的 GitHub 上找到。

那么,让我们开始吧!如果你糊涂了,我建议你在单独的选项卡中打开下面的视频。

◈  Python 的 Hello World 和字符串操作视频 [2]

开始 (先决条件)

首先在你的操作系统上安装 Anaconda (Python)。你可以从官方网站[3]下载 anaconda 并自行安装,或者你可以按照以下这些 anaconda 安装教程进行安装。

◈ 在 Windows 上安装 Anaconda: [链接 5 [4]◈ 在 Mac 上安装 Anaconda:  链接 [5]◈ 在 Ubuntu (Linux) 上安装 Anaconda: 链接 [6]

打开一个 Jupyter Notebook

打开你的终端(Mac)或命令行,并输入以下内容(请参考视频中的 1:16 处[7])来打开 Jupyter Notebook:


  1. jupyter notebook

打印语句/Hello World

在 Jupyter 的单元格中输入以下内容并按下 shift + 回车来执行代码。


  1. # This is a one line comment

  2. print('Hello World!')

640?wx_fmt=png

打印输出 “Hello World!”

字符串和字符串操作

字符串是 Python 类的一种特殊类型。作为对象,在类中,你可以使用 .methodName() 来调用字符串对象的方法。字符串类在 Python 中默认是可用的,所以你不需要 import 语句来使用字符串对象接口。


  1. # Create a variable

  2. # Variables are used to store information to be referenced

  3. # and manipulated in a computer program.

  4. firstVariable = 'Hello World'

  5. print(firstVariable)

640?wx_fmt=png

输出打印变量 firstVariable


  1. # Explore what various string methods

  2. print(firstVariable.lower())

  3. print(firstVariable.upper())

  4. print(firstVariable.title())

初识 Python:Hello World 和字符串操作 | Linux 中国_第2张图片

使用 .lower()、.upper() 和 title() 方法输出


  1. # Use the split method to convert your string into a list

  2. print(firstVariable.split(' '))

640?wx_fmt=png

使用 split 方法输出(此例中以空格分隔)


  1. # You can add strings together.

  2. a = "Fizz" + "Buzz"

  3. print(a)

640?wx_fmt=png

字符串连接

查询方法的功能

对于新程序员,他们经常问你如何知道每种方法的功能。Python 提供了两种方法来实现。

1、(在不在 Jupyter Notebook 中都可用)使用 help 查询每个方法的功能。

初识 Python:Hello World 和字符串操作 | Linux 中国_第3张图片

查询每个方法的功能

2.(Jupyter Notebook 专用)你也可以通过在方法之后添加问号来查找方法的功能。


  1. # To look up what each method does in jupyter (doesnt work outside of jupyter)

  2. firstVariable.lower?

初识 Python:Hello World 和字符串操作 | Linux 中国_第4张图片

在 Jupyter 中查找每个方法的功能

结束语

如果你对本文或在 YouTube 视频[2]的评论部分有任何疑问,请告诉我们。文章中的代码也可以在我的 GitHub[1] 上找到。本系列教程的第 2 部分是简单的数学操作[8]


via: https://www.codementor.io/mgalarny/python-hello-world-and-string-manipulation-gdgwd8ymp

作者:Michael[10] 译者:geekpi 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出


你可能感兴趣的:(初识 Python:Hello World 和字符串操作 | Linux 中国)