103-字串的定義和操作

像一棵海草海草海草發表於2024-07-16

Python中字串的定義和操作

str1 = 'I am learning Python 101!'
print(str1)
# I am learning Python 101!

str2 = "Python is fun. Machine learning is fun too."
print(str2)
# Python is fun. Machine learning is fun too.

# 使用加號 + 將多個字串連線起來
str4 = 'Hey, ' + 'James!'
print(str4)  
# 'Hey, James!'

# 使用乘號*將一個字串複製多次
str5 = 'Python is FUN! '  # 字串最後有一個空格
str6 = str5 * 3
print(str6)  
# 'Python is FUN! Python is FUN! Python is FUN!'

# 字串中的數字僅僅是字元
str7 = '123'
str8 = str7 * 3
print(str8)  
# 123123123

str9 = '456'
str10 = str9 + str7
print(str10)  
print(type(str10))
# 456123
# <class 'str'>


相關文章