print("Hello Python world!")
Hello Python world!
变量
Python将始终记录变量的最新值
message = "Hello Python world!"
print(message)
message = "Hello Python Crash Course world!"
print(message)
Hello Python world!
Hello Python Crash Course world!
字符串
用引号括起来的都是字符串,其中引号可以是单引号,也可以是双引号
print('I told my friend, "Python is my favorite language!"')
print("The language 'Python' is named after Monty Python,not the snake.")
print("One of Python's strengths is its diverse and supportive community.")
I told my friend, "Python is my favorite language!"
The language 'Python' is named after Monty Python,not the snake.
One of Python's strengths is its diverse and supportive community.
修改字符串的大小写
name = "aDA lovelace"
print(name.title())
print(name.upper())
print(name.lower())
Ada Lovelace
ADA LOVELACE
ada lovelace
合并字符串
first_name = "ada"
last_name = "lovelace"
full_name = first_name + " " + last_name
print(full_name)
print("Hello, " + full_name.title() + "!")
message = "Hello, " + full_name.title() + "!"
print(message)
ada lovelace
Hello, Ada Lovelace!
Hello, Ada Lovelace!
制表符或换行符添加空白
print("Python")
print("\tPython")
Python
Python
print("Language:\nPython\nC\nJavaScript")
print("\n")
print("Language:\n\tPython\n\tC\n\tJavaScript")
Language:
Python
C
JavaScript
Language:
Python
C
JavaScript
删除空白
favorite_language = ' Python '
print("\t临时性删除")
print(favorite_language.rstrip()) #临时删除字符串末尾空白
print(favorite_language.lstrip()) #临时删除字符串开头空白
print(favorite_language.strip()) #临时删除字符串两端空白
print(favorite_language)
print("\t永久性删除,给变量重新赋值")
favorite_language = favorite_language.lstrip()
print(favorite_language)
临时性删除
Python
Python
Python
Python
永久性删除,给变量重新赋值
Python
str() 函数将非字符串值转换为字符串
age = 23
message = "Happy " + str(age) + "rd Birthday!"
print(message)
Happy 23rd Birthday!