龙空技术网

Python第二章 变量和简单的数据类型-3(字符串)

数理模型王 111

前言:

现在各位老铁们对“字符串函数title作用”大致比较关注,你们都想要分析一些“字符串函数title作用”的相关资讯。那么小编同时在网络上搜集了一些关于“字符串函数title作用””的相关内容,希望咱们能喜欢,兄弟们快快来学习一下吧!

Strings 字符串

Because most programs define and gather some sort of data, and then do something useful with it, it helps to classify different types of data. The first data type we’ll look at is the string. Strings are quite simple at first glance, but you can use them in many different ways.

因为大多数程序会定义和收集一些种类的数据,然后用这些数据做一些有用的事情,所以有助于对不同类型的数据进行分类。我们将学习的第一种数据类型是字符串。字符串乍一看很简单,但它们有多种不同的使用方法。

A string is a series of characters. Anything inside quotes is considered a string in Python, and you can use single or double quotes around your strings like this:

"This is a string."

'This is also a string.'

字符串是一系列字符。在Python中,引号内的任何内容都被视为字符串,您可以使用单引号也可以使用双引号,如下所示:

"This is a string."

'This is also a string.'

This flexibility allows you to use quotes and apostrophes within your strings:

这种灵活性允许您在字符串中使用引号和撇号,例如:

'I told my friend, "Python is my favorite language!"'

"The language 'Python' is named after Monty Python, not the snake."

"One of Python's strengths is its diverse and supportive community."

Let’s explore some of the ways you can use strings.

让我们探索一下使用字符串的一些方法。

Changing Case in a String with Methods 使用方法更改字符串中的大小写

One of the simplest tasks you can do with strings is change the case of the words in a string. Look at the following code, and try to determine what’s happening.

name = "ada lovelace"

print(name.title())

使用字符串可以完成的最简单的任务之一是更改字符串中单词的大小写。查看以下代码,并尝试确定会发生什么。

Save this file as name.py, and then run it. You should see this output:

Ada Lovelace

将此文件保存为name.py,然后运行它。您应该会看到以下输出:

In this example, the variable name refers to the lowercase string "ada lovelace". The method title() appears after the variable in the print() call. A method is an action that Python can perform on a piece of data. The dot (.) after name in name.title() tells Python to make the title() method act on the variable name. Every method is followed by a set of parentheses, because methods often need additional information to do their work. That information is provided inside the parentheses. The title() function doesn’t need any additional information, so its parentheses are empty.

在本例中,变量名是小写字符串“ada lovelace”。方法title()出现在函数print()的变量之后。在Python中,方法是可以对一个数据进行的操作。name.title()告诉Python将title()方法作用于变量名。每个方法后面都有一组括号,因为方法通常需要额外的信息来完成它们的工作,这些额外信息放在括号内。title()函数不需要任何额外的信息,所以它的括号内是空的。

The title() method changes each word to title case, where each word begins with a capital letter. This is useful because you’ll often want to think of a name as a piece of information. For example, you might want your program to recognize the input values Ada, ADA, and ada as the same name, and display all of them as Ada.

title()方法将每个单词更改为title格式,其中每个单词以大写字母开头。这很有用,因为你经常想把名字看作一条信息。例如,您可能希望程序将输入值Ada、ADA和ada识别为相同的名称,并将它们全部显示为Ada。

Several other useful methods are available for dealing with case as well. For example, you can change a string to all uppercase or all lowercase letters like this:

name = "Ada Lovelace"

print(name.upper())

print(name.lower())

This will display the following:

ADA LOVELACE

ada lovelace

其他一些有用的方法也可用于处理大小写。例如,您可以将字符串更改为全大写或全小写字母,如下所示:

The lower() method is particularly useful for storing data. Many times you won’t want to trust the capitalization that your users provide, so you’ll convert strings to lowercase before storing them. Then when you want to display the information, you’ll use the case that makes the most sense for each string.

lower()方法对于存储数据特别有用。很多时候,你不想使用你的用户提供的大写字母,所以在存储字符串之前,你会将其转换为小写字母。然后,当您想要显示这些信息时,您将使用对每个字符串最有意义的大小写。

Using Variables in Strings 在字符串中使用变量

In some situations, you’ll want to use a variable’s value inside a string. For example, you might want two variables to represent a first name and a last name respectively, and then want to combine those values to display someone’s full name:

在某些情况下,您会希望在字符串中使用变量的值。例如,您可能希望用两个变量分别表示名字和姓氏,然后希望将这两个变量的值组合起来显示某人的全名:

first_name = "ada"

last_name = "lovelace"

1 full_name = f"{first_name} {last_name}"

print(full_name)

To insert a variable’s value into a string, place the letter f immediately before the opening quotation mark 1. Put braces around the name or names of any variable you want to use inside the string. Python will replace each variable with its value when the string is displayed.

要将变量的值插入字符串中,请将字母f直接放在开头引号之前。在字符串中在所要使用的任何变量名称周围加大括号。当字符串显示时,Python将用其值替换每个变量。

These strings are called f-strings. The f is for format, because Python formats the string by replacing the name of any variable in braces with its value. The output from the previous code is: ada lovelace

这些字符串被称为f字符串。f代表格式,因为Python通过将大括号中的任何变量的名称替换为变量的值来格式化字符串。之前代码的输出为:ada lovelace

You can do a lot with f-strings. For example, you can use f-strings to compose complete messages using the information associated with a variable, as shown here:

你可以用f字符串做很多事情。例如,您可以使用f字符串来使用与变量相关联的信息组合编写完整的消息,如下所示:

first_name = "ada"

last_name = "lovelace"

full_name = f"{first_name} {last_name}"

1 print(f"Hello, {full_name.title()}!")

The full name is used in a sentence that greets the user 1, and the title() method changes the name to title case. This code returns a simple but nicely formatted greeting: Hello, Ada Lovelace!

将全名用于问候用户的句子中,title()方法将名称更改为title大小写。此代码返回一个简单的但格式良好的问候语:

You can also use f-strings to compose a message, and then assign the entire message to a variable:

您也可以使用f字符串组成消息,然后将整个消息分配给一个变量:

first_name = "ada"

last_name = "lovelace"

full_name = f"{first_name} {last_name}"

1 message = f"Hello, {full_name.title()}!"

2 print(message)

This code displays the message Hello, Ada Lovelace! as well, but by assigning the message to a variable 1 we make the final print() call much simpler 2.

此代码同样显示消息Hello,Ada Lovelace!,但是是通过将消息分配给变量,使最终的print()函数调用变得更简单。

Note: F-strings were first introduced in Python 3.6. If you’re using Python 3.5 or earlier, you’ll need to use the format() method rather than this f syntax. To use format(), list the variables you want to use in the string inside the parentheses following format. Each variable is referred to by a set of braces; the braces will be filled by the values listed in parentheses in the order provided: full_name = "{} {}".format(first_name, last_name)

F字符串是在Python 3.6中首次引入的。如果您使用的是Python 3.5或更早的版本,则需要使用format()方法,而不是此f语法。要使用format(),请按照以下格式,在括号内的字符串中列出要使用的变量。每个变量都由一组大括号表示;大括号将按提供的顺序用括号中列出的值填充:full_name = "{} {}".format(first_name, last_name)

标签: #字符串函数title作用