python入门(1)
注释
单行注释
# 注释内容
pycharm快捷键 Ctrl + /
多行注释
'''
注释内容
'''
"""
注释内容
"""
两者一致
输出语句
print()
def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
"""
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
多个值 多个值用空格隔开 末尾为换号符号
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
"""
pass
print('hello world','python')
# hello world python
输出语句
input()
def input(*args, **kwargs): # real signature unknown
"""
Read a string from standard input. The trailing newline is stripped.
从标准输入中读取一个字符串
The prompt string, if given, is printed to standard output without a
trailing newline before reading input.
If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.
"""
pass
input这个函数在运行的过程中,会阻塞进程,等待用户的输入
string1 = input('请输入您需要打印的字符串:')
print(string1)
'''
=
赋值符号,将等号右边的内容赋值给等号的左边
'''
字符串
字符串格式
'hello world',"python"
# 单引号(' '),双引号(" ")都行
字符串的操作
查看类型
str1 = "hello"
print(type(str1))
# <class 'str'>
####切片与取值
str1 = 'i_love_python'
str2 = "i_love_python123"
下标取值
print('这是获取字符串的第一位字符',str1[0])
# i
print('这是获取字符串的第二位字符',str1[1])
# _
print('这是获取字符串的前二位字符',str1[0],str1[1])
# i_
print('这是获取字符串的前三位字符',str1[0:3])
# i_
#遵循c++的格式,左闭右开
切片
str[起始位置:结束位置:步长]
步长为正数,代表从开始向结束取值
步长为负数,代表从末尾向前取值
- 遵循c++的格式,左闭右开
- 步长为正(+),从左往右,为负(-),从右往左
- 步长默认为1(取一个字符)
# 练习:取出字符串的123
str2 = "i_love_python123"
print(str2[13:16])
# 123
print(str2[13:])
# 123
print(str2[-3:])
# 123
# 练习:步长
str2 = "i_love_python123"
print(str2[::1])
# i_love_python123
print(str2[::2]) #每两位取一位
# ilv_yhn2
print(str2[::-1]) #逆置整个字符串
# 321nohtyp_evol_i