使用 Python 从字符串中移除首个单词
Python 是一种常用的编程语言,用于许多不同的目的,例如 Web 开发、数据科学、机器学习以及执行许多不同的自动化流程。在本文中,我们将学习如何使用 Python 从字符串中移除首个单词。
从字符串中移除首个单词的不同方法
split 函数
在这种方法中,我们将字符串拆分为子字符串以移除首个字符串。我们将使用 split 函数来实现相同的目的。使用 split 函数从字符串中移除首个单词的语法如下所示
def initial_string(string): #The string is given as input
data = string.split() #The string is then split
if len(data) > 1: #If it has more than one element then it is joined
return ' '.join(data[1:])
else:
return ""
示例
让我们举个例子来理解它的用法
def initial_string(string): #The string is given as input
data = string.split() #The string is then split
if len(data) > 1: #If it has more than one element then it is joined
return ' '.join(data[1:])
else:
return ""
whole_string = "Today, We will work on Python."
new_string = initial_string(whole_string)
print(new_string)
输出
上述示例的输出如下所示
We will work on Python.
re 模块
re 模块提供正则表达式支持。它使用特定的模式来查找特定的字符串或子字符串。我们将使用相同的模式来查找首个字符串,然后将其移除。使用 re 模块从字符串中移除首个单词的语法如下所示
import re # Do not forget to import re module or else error might occur.
def initial_string(string): #The string is given as input
pattern = r'^\w+\s+' # A pattern is defined to find the initial string
return re.sub(pattern, '', string) # The initial string is substituted with an empty space
示例
上述方法的示例如下所示
import re # Do not forget to import re module or else error might occur.
def initial_string(string): #The string is given as input
pattern = r'^\w+\s+' # A pattern is defined to find the initial string
return re.sub(pattern, '', string) # The initial string is substituted with an empty space
whole_string = "Today We will work on Python."
new_string = initial_string(whole_string)
print(new_string) #Displaying the output after removing the initial string
输出
上述方法的输出如下所示
We will work on Python.
find 函数
find 函数用于使用其位置在字符串中查找特定的单词或元素。在这种方法中,我们将使用相同的函数来查找第一个位置的单词并将其移除。使用 find 函数移除首个单词的语法如下所示
def initial_string(string): #The string is given as input
first_word = string.find(' ') #Find() is used to find the initial word of the string and if space is found it returns back
if first_word != -1:
return string[first_word + 1:]
else:
return ""
示例
使用上述语法的示例如下所示
def initial_string(string): #The string is given as input
first_word = string.find(' ') #Find() is used to find the initial word of the string and if space is found it returns back
if first_word != -1:
return string[first_word + 1:]
else:
return ""
whole_string = "Today, We will work on Python."
new_string = initial_string(whole_string)
print(new_string) #Displaying the output after removing the initial string
输出
上述代码的输出如下所示
We will work on Python.
空格分隔符
我们使用字符串 split 函数将字符串拆分为两个不同的部分,然后移除具有多个元素的字符串,并移除首个单词。使用空格分隔符从字符串中移除首个单词的语法如下所示
def initial_string(string): #The string is given as input
first = string.split(' ', 1) #Second argument to split the string into two parts
if len(first) > 1: # Checking for more than 1 element
return first[1] #If more than 1 element is present it returns the first part
else:
return "" #If only 1 element is present it returns the second part
示例
上述方法的示例如下所示
def initial_string(string): #The string is given as input
first = string.split(' ', 1) #Second argument to split the string into two parts
if len(first) > 1: # Checking for more than 1 element
return first[1] #If more than 1 element is present it returns the first part
else:
return "" #If only 1 element is present it returns the second part
whole_string = "Today, We will work on Python."
new_string = initial_string(whole_string)
print(new_string) #Displaying the output after removing the initial string
输出
上述示例的输出如下所示
We will work on Python.
partition 方法
Python 的 partition 函数根据所选的分隔符将字符串划分为三个部分。因此,分隔符本身、分隔符之前的区域和分隔符之后的区域组合成一个元组。使用 partition 方法从字符串中移除首个单词的语法如下所示
def initial_string(string): #The string is given as input
_, separator, remainder = string.partition(' ') # The partition method is used to seprate the string at the first space in the string
if separator: #If a seprator will be found then it will return remainder or else a empty space will return
return remainder
else:
return ""
示例
使用上述方法的示例如下所示
def initial_string(string): #The string is given as input
_, separator, remainder = string.partition(' ') # The partition method is used to seprate the string at the first space in the string
if separator: #If a seprator will be found then it will return remainder or else a empty space will return
return remainder
else:
return ""
whole_string = "Today, We will work on Python."
new_string = initial_string(whole_string)
print(new_string) #Displaying the output after removing the initial string
输出
上述代码的输出如下所示
We will work on Python.
结论
使用 Python 从字符串中移除首个单词有很多不同的方法,初学者有必要了解所有这些编程方法,以便在不同领域的不同项目中工作时,能够在合适的问题中使用合适的逻辑。程序员可以使用他/她选择的方法,以及适合编程领域并缩短编程时间的方法。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP