search
HomeBackend DevelopmentPython TutorialPython zero-based introduction 5 String formatting and sequence built-in functions

First of all, let’s briefly talk about tuples. Tuples are jokingly said to be shackles of sequences. The reason why we say this is because tuples cannot perform a series of operations on their elements at will like sequences. Once defined After a tuple, the elements in the tuple cannot be changed at will.
Definition of tuple: tuple name = (,,,,,,,)
Note that the mark of the tuple is not (), but,
That’s all the ancestor said, let’s add some characters String formatting problem.
Don’t be too long-winded, let’s get straight to the essentials.

#字符串的格式化print("------Format函数的使用------")
#Format函数含有未知参数和关键字参数
#replacement字段用{}来代替
#前面叫做未知参数,后面叫做关键字参数
print("{0} love {1} {2}".format("I","FishC","com"))print("\n")
#关键字参数
#下面这样写是错误的,因为编译器并不知道a,b,c分别对应谁
#print("{a} love {b} {c}".format("I","FishC","com"))
print("{a} love {b} {c}".format(a="I",b="FishC",c="com"))
#或者将关字参数与未知参数混合起来使用,但是
#未知参数要在关键字参数的前面
print("\n")print("{0} love {b} {c}".format("I",b="FishC",c="com"))
#使用转义字符打印花括号print("\n")print("{{0}}".format("转义打印花括号"))
#冒号表示格式化的开始print("{0:.1f}{1}".format(27.658,"GB"))

The output result is:

Python zero-based introduction 5 String formatting and sequence built-in functions

#Let’s talk about sequence-related issues. Sequence is the collective name for lists, tuples, and strings. The reason why they are The three are put together because there are some commonalities between them. Today we are mainly learning about the built-in functions of sequences, and we will go directly to the practical information.

#序列序列
#列表 元组 字符串 统称为序列
#list()函数作用是将一个可迭代对象转换为列表
#若没有参数,则生成一个空列表
#若有一个参数,该参数是迭代器print("使用list()函数生成一个空的列表")
a=list();
print(a,"\n")#list函数的过程可以理解为先建立一个空的序列
#然后用for循环采用索引函数将元素移到新的序列中
print("使用list(iterator)函数生成一个非空的列表")
a="I Love You"atolist=list(a)
print(atolist)
print("\n")#tuple([iterator])把一个可迭代的对象转换为元组atotuple=tuple(a)
print(atotuple)
print("\n")#str(obj)
函数:将obj对象转换为字符串,强制类型转换#len(sub)
函数:返回序列的长度#max(),min()函数保证序列中的数据类型统一 
#max()
函数:返回序列或者参数集合中的最大值print(max(1,2,3,4,5))
#min()
函数:返回序列或者参数集合中的最大值#sum(iterable[,start=0])返回序列iterable和可选参数start的总和#数列中的数据类型也必须保持一致,必须是数字print("\n")
tuple1=(1,2,3,4,5)
print(sum(tuple1))
print("\n")#**sorted**函数实现对序列的排序,默认是从小到大#注意与列表的内置函数sort区分list1=[1,5,4,3,6]
list1=sorted(list1)
print(list1)
print("\n")#**reversed()**实现序列元素的逆转,但是返回的不是序列#注意与列表的内置函数reverse()区分#而是一个迭代的对象print(reversed(list1))
print("\n")#我们可以将返回的迭代器的对象作为list(iterator)的参数#返回一个列表print(list(reversed(list1)))
print("\n")#zip合成序列a=[1,2,3,4,5,6,7,8]
b=["a","b","c","d"]
zip(a,b)
print(list(zip(a,b)))

The program runs as follows:

Python zero-based introduction 5 String formatting and sequence built-in functions

Python zero-based introduction 5 String formatting and sequence built-in functions

#The main thing is to remember the usage of the function, there is nothing that requires a lot of brainpower Cell stuff.


The above is the content of Python Zero Basic Introduction to String Formatting and Sequence Built-in Functions. For more related content, please pay attention to the PHP Chinese website (www.php. cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.