Python for Developers 入门
Jasmin Ludolf
Senior Data Science Content Developer

# 可行
ingredient_name = 'San Marzano tomatoes'
# 也可行
ingredient_name = "San Marzano tomatoes"
' = 撇号 = 单引号# 单引号字符串中包含撇号
ingredient_name = 'Chef's special seasoning'
print(ingredient_name)
SyntaxError: invalid syntax.
# 双引号字符串中包含撇号
ingredient_name = "Chef's special seasoning"
print(ingredient_name)
Chef's special seasoning
recipe_step = "Heat olive oil in a large pan and sauté garlic until fragrant"
# 跨多行创建字符串变量
recipe_instructions = """1. Bring a large pot of salted water to boil and cook pasta
2. Heat olive oil in a pan and sauté minced garlic until fragrant
3. Add chopped tomatoes and simmer for 10 minutes
4. Toss cooked pasta with tomato sauce and fresh basil leaves
"""
"""text""":多行字符串方法:仅适用于特定数据类型的函数
str 方法
# 调用字符串方法
string_variable.method()
.replace(text_to_be_replaced, text_to_change_it_to)welcome_message = "Welcome to the recipe scaler, George" welcome_message = welcome_message.replace("George", "John")print(welcome_message)
Welcome to the recipe scaler, John
ingredient_name = "Basil Leaves"# 转为小写 ingredient_name = ingredient_name.lower() print(ingredient_name)
basil leaves
# 转为大写
ingredient_name = ingredient_name.upper()
print(ingredient_name)
BASIL LEAVES
Python for Developers 入门