Python中字符串的創(chuàng)建方法有以下幾種:
str1 = 'Hello World'
str2 = "Python is awesome"
str3 = '''This is a
multi-line string'''
str4 = 'I\'m a programmer'
str5 = "He said, \"Hello World\""
str6 = r'C:\Users\Username\Desktop'
name = 'Alice'
age = 25
str7 = 'My name is {} and I am {} years old'.format(name, age)
str8 = 'Hello' + ' ' + 'World'
str9 = 'Python' * 3
需要注意的是,字符串是不可變的,即不能對字符串進(jìn)行修改,但可以通過創(chuàng)建新的字符串來實現(xiàn)對字符串的修改。