#字符串的拼接
str1 = 'hello'
str2 = " "
str3 = "world"
str4 = str1 + str2 + str3
print(str4) #结果:hello world
#字符串获取字符
str1 = 'hello'
print(str1[3]) #获取字符串的第三个字字符,结果为l,注意数数从0开始数的
print(str1[1:3]) #从字符串中的第1个字符开始获取到第3个结束,运行结果为el,注意数数从0开始数的
#索引字符串中是否含有某个字符
str1 = "hello world!"
if "hello"in str1:
print("str1中含有hello")
else:
print("str1中不含hello")
'''
#读取个人信息并合成
name = str(input("请输入你的姓名:"))
age = int(input("请输入你的年龄:"))
height = float(input("请输入你的身高:"))
print("%s is %d years old , he is %.2fcm height."%(name,age,height)) #%.2d %.2f表示需要的精度
'''
成员运算符:
格式:x in seq x not in seq
in:如果在指定的序列中,则返回真
not in:如果不在序列中,则返回假
身份运算符:
格式:obj1 is obj2 obj1 is not obj2
is 判断两个标识符身份表示同一个对象,引用相同返回为真,否则为假
is not 判断两个标识符引用是否为不同对象,不同返回真,否则返回假
运算符的优先级:
** 指数
~ + - 按位取反和医院加减号即正负号
* / % //
+ - 加减号法
<< >>
&
| ^
< > <= >=
== !=
= *= /= %= //= += -= **=
is is not
in not in
字符串:
以单引号或双引号括起来的任意文本,但是引号本身是一种表现形式,不属于字符串的一部分
注意:如果字符串本身带双引号,则外侧不能再用双引号,应用单引号括起来
如果字符串本身带单引号,则外部用双引号括起来
例如:"hello world!"
'hello world'
'hello "world"'
"hello 'world'"
创建字符串:
str1 = 'hello'
str2 = "world"
字符串的拼接:
str3 = str1 + str2
重复字符串:str1 * 3 相当于输出三次str1
通过索引获取字符串中的字符,索引从0开始:
str1 = hello
print(str1[1])
判断字符串中是否存在某些内容:
格式: in not in
例如:str1 = 'hello'
print('ll'in str1)
格式化输出:
%s 格式化字符串
%d 格式化整数
%f 格式化浮点数,可以指定小数后的精度
例如:name = str(input("请输入你的姓名:"))
age = int(input("请输入你的年龄:"))
height = float(input("请输入你的身高:"))
print("%s is %d years old , he is %.2fcm height."%(name,age,height))
注意:%.2d %.2f表示需要的精度
转义字符:
反斜杠 \
单引号 ‘
双引号 “
换行 #\n
多行换行 ''' '''
例如:若要打印#\n 可用print("hello world \\n")