寫一個 Python 程序,用一個實例將一個字符串複製到另一個字符串。
Python 程序將字符串複製到另一個示例 1
與其他語言不同,您可以使用等於運算符將一個字符串分配給另一個字符串。或者,您可以從頭到尾切片,並將其保存在另一個字符串中。這個 Python 程序允許用戶輸入任何字符串值。接下來,我們使用上面指定的方法來複制用戶指定的字符串。
# Python Program to Copy a String
str1 = input("Please Enter Your Own String : ")
str2 = str1
str3 = str1[:]
print("The Final String : Str2 = ", str2)
print("The Final String : Str3 = = ", str3)
Python 字符串複製輸出
Please Enter Your Own String : Hi Guys
The Final String : Str2 = Hi Guys
The Final String : Str3 = = Hi Guys
Python 複製字符串示例 2
在這個程序中,我們使用 For 循環來迭代字符串中的每個字符,並將它們複製到新的字符串中。
# Python Program to Copy a String
str1 = input("Please Enter Your Own String : ")
str2 = ''
for i in str1:
str2 = str2 + i
print("The Final String : Str2 = ", str2)
Python 字符串複製示例 3
這個 Python 程序和上面的例子一樣。然而,我們使用帶有範圍的 For 循環來複制一個字符串。
# Python Program to Copy a String
str1 = input("Please Enter Your Own String : ")
str2 = ''
for i in range(len(str1)):
str2 = str2 + str1[i]
print("The Final String : Str2 = ", str2)
Python 字符串複製輸出
Please Enter Your Own String : Python Programs With Examples
The Final String : Str2 = Python Programs With Examples
>>>
Please Enter Your Own String : Hello World
The Final String : Str2 = Hello World
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/282700.html