字符串 本是字符串数据类型。任何以文本形式写入的数据类型都是字符串。单引号、双引号或三引号下的任何数据都是字符串。有不同的字符串方法和内置函数来处理字符串数据类型。要检查字符串的长度,请使用 len() 方法。 创建字符串
letter = "p" print(letter) # p print(len(letter)) #1 greeting = 'Hello, World!' # String could be made using a single or double quote,"Hello, World!" print(greeting) # Hello, World! print(len(greeting)) # 13 sentence = "I hope you are enjoying 30 days of Python Challenge" print(sentence)
字符串 ‘’’ 和 “”" 的创建方式
multiline_string = '''I am a teacher and enjoy teaching. I didn't find anything as rewarding as empowering people. That is why I created 30 days of python.''' print(multiline_string) # Another way of doing the same thing multiline_string = """I am a teacher and enjoy teaching. I didn't find anything as rewarding as empowering people. That is why I created 30 days of python.""" print(multiline_string)
连接字符串 连接字符串使用+
first_name = "abc" last_name = "efg" space = "" full_name = first_name + space + last_name print(full_name) # abc efg print(len(full_name)) #7 print(len(first_name)> len(last_name)) # False
字符串中的转义序列 在 Python 和其他编程语言中, 后跟一个字符是一个转义序列。让我们看看最常见的转义字符:
n 换行
t 制表符 空格8个
\ 反斜杠
' 单引号
" 双引号
print("abnc")
print("atbtc")
print("abc\")
print(""ab"c")
print("'ab'c")
结果:
ab
c
a b c
abc
"ab"c
'ab'c
字符串格式(旧) 在Python中,采用的格式化方式和C语言是一致的,用%实现,举例如下:
%s - 字符串(或任何具有字符串表示的对象,如数字)
%d - 整数
%f - 浮点数
“%. number of digits f” - 具有固定精度的浮点数
简单例子:
h = [1,3,4,5,6,8,9]
name = ["c","b","a"]
for hh in h:
aa="亲爱的 %s 你好!你月的话费是%d" %("a",hh)
print(aa)
亲爱的 a 你好!你月的话费是1
亲爱的 a 你好!你月的话费是3
亲爱的 a 你好!你月的话费是4
亲爱的 a 你好!你月的话费是5
亲爱的 a 你好!你月的话费是6
亲爱的 a 你好!你月的话费是8
亲爱的 a 你好!你月的话费是9
first_name = 'Asabeneh'
last_name = 'Yetayeh'
language = 'Python'
formated_string = "Im am %s. I%s teach %s"%(first_name,last_name,language)
print(formated_string)
输出结果:Im am Asabeneh. I Yetayeh teach Python
%d 整数 和 %.number of difitsf 浮点
radius = 10
pi = 3.14
area = pi * radius ** 2
formated_string = 'The area of circle with a radius %d is %.2f.' %(radius, area)
print(formated_string)
# The area of circle with a radius 10 is 314.00.
formated_string = 'The area of circle with a radius %05d is %.2f.' %(radius, area)
print(formated_string)
# The area of circle with a radius 00010 is 314.00.
其中:%05d 表示总长度 为5 ,000 10
%.2f 表示314.00
python_libraries = ['Django', 'Flask', 'NumPy', 'Matplotlib','Pandas']
formated_string = 'The following are python libraries:%s' % (python_libraries)
print(formated_string) # "The following are python libraries:['Django', 'Flask', 'NumPy', 'Matplotlib','Pandas']"
字符串(新) python3 才能{ } 新的字符串格式
first_name = 'Asabeneh'
last_name = 'Yetayeh'
language = 'Python'
formated_string = 'I am {} {}. I teach {}'.format(first_name, last_name, language)
print(formated_string)
a = 4
b = 3
print('{} + {} = {}'.format(a, b, a + b))
print('{} - {} = {}'.format(a, b, a - b))
print('{} * {} = {}'.format(a, b, a * b))
print('{} / {} = {:.2f}'.format(a, b, a / b)) # 限制为小数点后两位
print('{} % {} = {}'.format(a, b, a % b))
print('{} // {} = {}'.format(a, b, a // b))
print('{} ** {} = {}'.format(a, b, a ** b))
# output
4 + 3 = 7
4 - 3 = 1
4 * 3 = 12
4 / 3 = 1.33
4 % 3 = 1
4 // 3 = 1
4 ** 3 = 64
# Strings and numbers
radius = 10
pi = 3.14
area = pi * radius ** 2
formated_string = 'The area of a circle with a radius {} is {:.2f}.'.format(radius, area) # 2 digits after decimal
print(formated_string)
字符串插值(f-strings) 插值python3.6+ 使用f"{}" 进行插值
a = 4
b = 3
print(f'{a} + {b} = {a +b}')
print(f'{a} - {b} = {a - b}')
print(f'{a} * {b} = {a * b}')
print(f'{a} / {b} = {a / b:.2f}')
print(f'{a} % {b} = {a % b}')
print(f'{a} // {b} = {a // b}')
print(f'{a} ** {b} = {a ** b}')
python 字符串作为字符序列Python 字符串是字符序列,并与其他 Python 有序对象序列(列表和元组)共享它们的基本访问方法。从字符串(以及任何序列中的单个成员)中提取单个字符的最简单方法是将它们解包到相应的变量中。
拆包 language = 'Python' 包 a,b,c,d,e,f = language print(a) # P print(b) # y print(c) # t print(d) # h print(e) # o print(f) # n
搜索引用访问字符串中的字符 在编程中,计数从零开始。因此,字符串的第一个字母的索引为零,字符串的最后一个字母是字符串的长度减一。
language = 'Python' first_letter = language[0] print(first_letter) # P second_letter = language[1] print(second_letter) # y last_index = len(language) - 1 last_letter = language[last_index] print(last_letter) # n 没有就最后一个啦
如果我们想从右端开始,我们可以使用负索引。-1 是最后一个索引。
language = 'Python' first_letter = language[-1] print(first_letter) # n second_letter = language[-2] print(second_letter) # 0



