这测试字符串,这段没有意义
那测试字符串,那段没有意义
lello world
$12 equals to 12 pounds
$4 equals to 1 pounds
2. 使用fstring方式格式化字符串
1. 在Python语言中哪种格式化方式直接使用变量
fstring
2. 请用代码描述如何使用fstring格式化字符串
name ='Bill'
age =20defgetAge():return21
s = f'He is {name}, age is {age}. {getAge()} years old next year'print(s)
He is Bill, age is 20. 21 years old next year
classPerson:def__init__(self):
self.name ='Mike'
self.age=30defgetAge(self):return self.age +1
person = Person()
s = f'He is {person.name}, age is {person.age}. {person.getAge()} years old next year'print(s)
l
lo wo
world
hlowrd
xyzxyzxyzxyzxyzxyzxyzxyzxyzxyz
False
True
True
11
x
z
4. 向字符串的format方法传递参数有几种方式
1. 向字符串的format方法传递参数有几种方式
三种
默认方式,传入的参数与{}一一对应,命名参数和位置参数
2. 详细描述字符串的format方法如何格式化字符串
#coding=utf-8
s1 ='Today is {},the temperature is {} degree.'print(s1.format('Tuesday',18))
s2 ='Today is {day},the temperature is {temp} degree.'print(s2.format(temp=-9,day='Monday'))
s3 ='Today is {day},{},the temperature is {temp} degree.'print(s3.format(1,day='Wednesday',temp=10))
s4 ='Today is {day},{1},the {0} temperature is {temp} degree.'print(s4.format('day',3,day='Wednesday',temp=10))classPerson:def__init__(self):
self.name ='Mike'
self.age=30defgetName(self):return self.name
person = Person()
s ='He is {person.name}, age is {person.age}.'print(s.format(person=person))
Today is Tuesday,the temperature is 18 degree.
Today is Monday,the temperature is -9 degree.
Today is Wednesday,1,the temperature is 10 degree.
Today is Wednesday,3,the day temperature is 10 degree.
He is Mike, age is 30.