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教程

计算机教程

大数据教程

开发工具教程