Python上课笔记2:字符串

字符串笔记

字符串去空白

 >>>favorite_language = 'python ' 
 #右边有空白
 >>> favorite_language.rstrip()  'python' 

你还可以剔除字符串开头的空白,或同时剔除字符串两端的空白。为此,可分别使用方法lstrip()strip()

将其他型变为字符串

age = 23 
message = "Happy " + age + "rd Birthday!" print(message)
#it is wrong because age is not string
message = "Happy " + str(age) + "rd Birthday!" 

作业

多条简单消息:

将一条消息存储到变量中,将其打印出来;再将变量的值修改为一条新消息,并将其打印出来。

message = "Hello Python Crash Course reader!" 
print(message)
message = "hahaha"
print(message)

The result is

Hello Python Crash Course reader!
hahaha

你可能感兴趣的:(python)