1、来源
1、input()接收到的.
2、文件的读写.
3、爬虫的数据.
2、定义
'hello'
"hello"
'''hello'''
"""hello"""
在代码输入换行的情况下–>
前两个支持代码缩进换行,输出不换行;
后两者代码无缩进换行,输出换行;
示例:
str1 = 'hello' \
'word!'
str2 = "hello" \
"python"
str3 = '''hello
java'''
str4 = """hello
c#"""
print(str1,str2,str3,str4)
输出:
helloword! hellopython hello
java hello
c#
3、字符串输出
示例:
print("hello")
str="hello"
print(str)
print("%s" %str)
print(f'{str}')
输出:
hello
hello
hello
hello
反斜杠可以用来转义,使用r可以让反斜杠不发生转义。
示例:
print('hello\nword')
print(r'hello\nword')
输出:
hello
word
hello\nword
4、字符串输入
password = input("请输入密码:")
5、小标/索引
字符串依次分配从0开始的标号
print(password[0]) #输出第一个字符
字符串 | a | b | c | d | e | f |
---|---|---|---|---|---|---|
从前面索引 | 0 | 1 | 2 | 3 | 4 | 5 |
从后面索引 | -6 | -5 | -4 | -3 | -2 | -1 |
str = "hello"
print(str[0])
h
6、切片
指对操作的对象截取其中一部分的操作。
字符串、列表、元组都支持切片操作。
语法: 序列[开始位置下标:结束位置下标:步长]
取不到结束位置下标对应的数据。
各部分正负整数均可。
步长是选取间隔,正负整数均可,步长为负数时倒序选取