letter ="p"print(letter)# pprint(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 efgprint(len(full_name))#7print(len(first_name)>len(last_name))# False
字符串中的转义序列
在 Python 和其他编程语言中,\ 后跟一个字符是一个转义序列。让我们看看最常见的转义字符:
\n 换行
\t 制表符 空格8个
\\ 反斜杠
\' 单引号
\" 双引号
print("ab\nc")print("a\tb\tc")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,00010%.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 =3print('{} + {} = {}'.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))# output4+3=74-3=14*3=124/3=1.334%3=14//3=14**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 decimalprint(formated_string)
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