Python 切片是什么

Python 切片是什么

Python中的切片是从字符串中获取子字符串的操作。切片的范围由参数来设置,即开始、结束和步长。

语法

让我们来看一下语法

# slicing from index start to index stop-1
arr[start:stop]

# slicing from index start to the end
arr[start:]

# slicing from the beginning to index stop - 1
arr[:stop]

# slicing from the index start to index stop, by skipping step
arr[start:stop:step]

切片示例

在这个示例中,我们将对字符串进行起始位置、结束位置和跳步等切片操作。-

myStr = 'Hello! How are you?'
print("String = ",myStr)

# Slice
print("Slicing from index start to index stop-1 = ",myStr[5:8])
print("Slicing from index start to the end = ",myStr[3:])
print("Slicing from the beginning to index stop - 1 = ",myStr[:5])
print("Slicing from the index start to index stop, by skipping step = ",myStr[5:11:2])

输出

String =  Hello! How are you?
Slicing from index start to index stop-1 =  ! H
Slicing from index start to the end =  lo! How are you?
Slicing from the beginning to index stop - 1 =  Hello
Slicing from the index start to index stop, by skipping step =  !Hw

切割字符串

让我们来切割一个字符串 –

# Create a String
myStr = 'Hello! How are you?'

# Display the String
print("String = ",myStr)

# Slice the string
print("String after slicing = ",myStr[8:11])

输出

String =  Hello! How are you?
String after slicing =  ow

使用步长切割字符串

步长用于设置切割时每个索引之间的增量 –

# Create a String
myStr = 'Hello! How are you?'

# Display the String
print("String = ",myStr)

# Slice the string with step
print("String after slicing = ",myStr[8:15:2])

输出

String =  Hello! How are you?
String after slicing =  o r

切片元组

我们可以对元组进行切片操作 –

# Create a Tuple
mytuple = ("Tim", "Mark", "Harry", "Anthony", "Forrest", "Alex", "Rock")

# Display the Tuple
print("Tuple = ",mytuple)

# Slice the Tuple
print("Tuple after slicing = ",mytuple[3:5])

输出

Tuple =  ('Tim', 'Mark', 'Harry', 'Anthony', 'Forrest', 'Alex', 'Rock')
Tuple after slicing =  ('Anthony', 'Forrest')

切片元组带有步长

我们可以切片元组的部分,并使用步长参数设置每个索引之间的增量进行切片 –

# Create a Tuple
mytuple = ("Tim", "Mark", "Harry", "Anthony", "Forrest", "Alex", "Rock", "Paul", "David", "Steve")

# Display the Tuple
print("Tuple = ",mytuple)

# Slice the Tuple with step 2
print("Tuple after slicing = ",mytuple[2:8:2])

输出

Tuple =  ('Tim', 'Mark', 'Harry', 'Anthony', 'Forrest', 'Alex', 'Rock', 'Paul', 'David', 'Steve')
Tuple after slicing =  ('Harry', 'Forrest', 'Rock')

切割列表

# Create a List
myList = ['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

# Display the List
print("List = ",myList)

# Slice the List
print("List after slicing = ",myList[3:6])

输出

List =  ['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
List after slicing =  ['s', 't', 'u']

使用步长来切片列表

我们可以对列表进行切片,并且还可以使用步长参数设置切片时每个索引之间的增量 –

# Create a List
myList = ['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

# Display the List
print("List = ",myList)

# Slice the List with step 2
print("List after slicing = ",myList[3:9:2])

输出

List =  ['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
List after slicing =  ['s', 'u', 'w']

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程