编写一个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程序与上述示例相同。但是,我们使用带Range的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