温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python Hello World和字符串操作的实例分析

发布时间:2021-10-26 17:33:32 来源:亿速云 阅读:217 作者:柒染 栏目:编程语言

Python Hello World和字符串操作的实例分析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。开

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

  • Python 的 Hello World 和字符串操作视频

开始 (先决条件)

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

  • 在 Windows 上安装 Anaconda: [链接5

  • 在 Mac 上安装 Anaconda: 链接

  • 在 Ubuntu (Linux) 上安装 Anaconda:链接

打开一个 Jupyter Notebook

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

jupyter notebook

打印语句/Hello World

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

# This is a one line commentprint('Hello World!')

Python Hello World和字符串操作的实例分析

打印输出 “Hello World!”

字符串和字符串操作

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

# Create a variable# Variables are used to store information to be referenced# and manipulated in a computer program.firstVariable = 'Hello World'print(firstVariable)

Python Hello World和字符串操作的实例分析

输出打印变量 firstVariable

# Explore what various string methodsprint(firstVariable.lower())print(firstVariable.upper())print(firstVariable.title())

Python Hello World和字符串操作的实例分析

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

# Use the split method to convert your string into a listprint(firstVariable.split(' '))

Python Hello World和字符串操作的实例分析

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

# You can add strings together.a = "Fizz" + "Buzz"print(a)

Python Hello World和字符串操作的实例分析

字符串连接

查询方法的功能

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

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

Python Hello World和字符串操作的实例分析

查询每个方法的功能

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

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

Python Hello World和字符串操作的实例分析

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

关于Python Hello World和字符串操作的实例分析问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI