Python – 交替字元新增
簡介
Python 語言屬於面向物件程式設計 (OOPS) 概念,它會在不檢查錯誤的情況下立即執行程式碼。與其他語言相比,它因其自身的優勢而擁有獨特的地位,例如易於使用簡單的語法和語句進行編碼。字串由字元組成,字元新增是用於更改字串中字元的字串操作技術之一。然後將這些修改後的字元組合在一起形成一個新的字串。
交替字元新增
方法
方法 1 - 使用遞迴函式。
方法 2 - 使用 for 迴圈。
方法 3 - 使用 join() 方法。
方法 1:使用遞迴函式進行 Python 交替字元新增程式
遞迴函式用於獲取兩個輸入字串,並在交替字元字串後返回一個字串。
演算法
步驟 1 - 使用字串值初始化兩個變數。
步驟 2 - 使用兩個引數定義一個函式。
步驟 3 - 使用 if 語句檢查字串是否為空。
步驟 4 - 另一個條件是字串2的長度應大於字串1。
步驟 5 - return 語句具有 join() 和 zip() 函式以及元素的切片。
步驟 6 - 第一個元素 S1[0] 新增到 S2[0],S1[1] 新增到 S2[1],此過程將持續到所有元素都新增完畢。
步驟 7 - print 語句將返回修改後的字元。
示例
#initializing the two variables to store the string value
string1 = "Python"
string2 = "Golang"
#function is defined with two parameters of string data type
def add_alter(string_1: str, string_2: str) -> str:
#check if two given strings are empty
if not (string_1 or string_2):
return ""
#checks whether the length of the string2 is greater than the string1
elif len(string_2) > len(string_1): # We make sure that the first variable holds a greater value than the second.
temp_var = string_2
string_2= string_1
else:
temp_var= string_2
#using the join() and zip() function to alter the characters of the string
return ''.join([char[0] + char[1] for char in zip(string_1, string_2)]) + temp_var[len(string_2):]
#the function is called as it is a recursive function
new_str = add_alter("Python", "Golang")
#returns the new string after altering the characters
print("String alternate character addition:",new_str)
輸出
String alternate character addition: PGyotlhaonng
方法 2:使用 for 迴圈進行 Python 交替字元新增程式
for 迴圈將每個字元與來自字串1的對應字元一起新增到宣告為“new_str”的空字串中。
演算法
步驟 1 - 定義兩個帶有字串值的變數。
步驟 2 - 使用 if 語句驗證字串是否為空。
步驟 3 - 如果字串2的長度應大於字串1,則將部署下一個條件。
步驟 4 - for 迴圈用於迭代字串的每個字元。
步驟 5 - 如果字串很長,則將多餘的字元儲存在 tem_var 中。
步驟 6 - print 語句將返回修改後的字元。
示例
#initializing the two variables to store the string value
string1 = "Python"
string2 = "Golang"
#initializing the empty strings
new_str = ""
temp_var = ""
#check if two given strings are empty
if not (string1 or string2):
new_str = ""
#checks whether the length of the string2 is greater than the string1
elif len(string2) > len(string1):
temp_var = string2
string2 = string1
else:
temp_var = string2
#for loop is used to iterate through the characters of the string
for i in range(len(string2)):
new_str += string1[i] + string2[i]
new_str += temp_var[len(string2):]
#returns the new string after altering the characters
print("String alternate character addition:", new_str)
輸出
String alternate character addition: PGyotlhaonng
方法 3:使用 join() 方法進行 Python 交替字元新增程式
zip() 函式用於將兩個給定的字串組合在一起,join() 函式使用列表推導式附加變數和 zip() 函式的所有元素。
演算法
步驟 1 - 建立兩個包含字串值的變數。
步驟 2 - return 語句具有 join() 和 zip() 函式以新增字串的字元。
步驟 3 - 如果字串2的長度更大,則將多餘的字元新增到新字串的末尾。
步驟 4 - print 語句將返回修改後的字元。
示例
#initializing the two variables to store the string value
string1 = "Python"
string2 = "Golang"
#using the join() and zip() function to alter the characters of the string
new_str = ''.join([a + b for a, b in zip(string1, string2)]) + string2[len(string1):]
#returns the new string after altering the characters
print("String alternate character addition:", new_str)
輸出
String alternate character addition: PGyotlhaonng
結論
Python 是一種靈活且高階的語言,使用者易於理解。在當今世界,對於資料量大的組織來說,管理資料是最具挑戰性的任務,隨著資料科學和機器學習的發展,訪問資料變得更加容易。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP