Python3 基础语法

注释方式

# 这是注释
'''
这是注释
'''
"""
这是注释
"""

字符串

str='hello'

print(str)                 # 输出字符串
print(str[0:-1])           # 输出第一个到倒数第二个的所有字符
print(str[0])              # 输出字符串第一个字符
print(str[2:4])            # 输出从第三个开始到第四个的字符
print(str[2:])             # 输出从第三个开始的所有字符
print(str * 2)             # 输出字符串两次
print(str + ' furong')     # 连接字符串
print('hello
furong')     # 使用转义字符
print(r'hello
furong')    # 在字符串前面添加一个 r,表示原始字符串,不会发生转义

运行结果

hello
hell
h
ll
llo
hellohello
hello furong
hello
furong
hello
furong

Print 输出
print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end=” “

x="a"
y="b"

print( x, end=" " )
print( y, end=" " )

运行结果

a b 
原文地址:https://www.cnblogs.com/zhangxuechao/p/11709407.html