使用 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