1- """ 或 ''' 来指定多行字符串
example:
s='''this is a multi-line string.
This is the second line'''
print:
this is a multi-line string.
This is the second line
2- 从其他信息中构建字符串,format()
example:
age = 20
name = 'Swaroop'
print('{0} was {1} years old when he wrote this book'.format(name, age))
print:
Swaroop was 20 years old when he wrote this book
3-转义字符
\ 指定单引号: 'What\'s your name?'
\\ 来指定反斜杠本身
r 或 R 来指定一个原始(Raw)字符串: '\\1' 和 r'\1' 输出的结果相同
4-for循环
for i in range(1, 5):
print(i)
print:
1
2
3
4
5- 函数:
用 def 来定义函数
def say_hello():
# 该块属于这一函数
print('hello world')
# 函数结束
say_hello() # 调用函数
本文详细介绍了Python中的多行字符串定义、字符串格式化、转义字符的使用、for循环及函数定义与调用等核心概念。通过具体示例,帮助读者深入理解Python的基本语法结构。
1364

被折叠的 条评论
为什么被折叠?



