@浙大疏锦行
字符串的操作
- 字符串拼接:使用‘+’运算符;使用join() 对列表元素进行拼接;格式化方法
- 字符串长度:len()
- 字符串切片:索引的使用
str1 = 'Hello'
str2 = 'Python'
#字符串拼接
greeting = str1 + ' ' + str2 #" ".join([str1,str2])
length = len(greeting)
first_char = greeting[0]
print(f"拼接结果:{greeting}")
print(f'字符串长度:{length}')
print(f'第一个字符:{first_char}')
比较运算
score_a = 75
score_b = 90
is_a_higher = score_a > score_b
is_a_lower_or_equal = score_a <= score_b
is_different = score_a != score_b
print(f'{score_a} 是否大于 {score_b}:{is_a_higher}')
print(f'{score_a} 是否小于等于 {score_b}:{is_a_lower_or_equal}')
print(f'{score_a} 是否不等于 {score_b}:{is_different}')