開始使用免費開始

用字串做運算

和 R 類似,你可以把 Python 當計算機來對數值做運算。 例如,相加兩個值 42 + 3.14,或相乘 7 * 9。 不過,和 R 不同的是,Python 允許你在非數值上使用某些數學運算子。 當你對字串使用 + 時,會把它們接在一起:

print('hello' + 'world')

'helloworld'

把字串乘上一個整數 n,會把該字串重複 n 次:

print('hello' * 3)

'hellohellohello'

最後,若直接把兩個字串相鄰放在一起,也會被串接:

print('hello' 'world')

'helloworld'

本練習屬於課程

給 R 使用者的 Python

檢視課程

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Add 'the quick' to 'brown fox'
print('the quick'  ____)
編輯並執行程式碼