第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在Python的同一行上打印變量和字符串?

如何在Python的同一行上打印變量和字符串?

qq_笑_17 2021-03-29 17:05:45
我正在使用python算出如果一個(gè)孩子每7秒出生一次,那么5年內(nèi)將有多少個(gè)孩子出生。問題出在我的最后一行。當(dāng)我在文本的任何一側(cè)打印文本時(shí),如何使它工作?這是我的代碼:currentPop = 312032486oneYear = 365hours = 24minutes = 60seconds = 60# seconds in a single daysecondsInDay = hours * minutes * seconds# seconds in a yearsecondsInYear = secondsInDay * oneYearfiveYears = secondsInYear * 5#Seconds in 5 yearsprint fiveYears# fiveYears in seconds, divided by 7 secondsbirths = fiveYears // 7print "If there was a birth every 7 seconds, there would be: " births "births"
查看完整描述

4 回答

?
眼眸繁星

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊

使用,分隔字符串和變量,同時(shí)打?。?/p>


print("If there was a birth every 7 seconds, there would be: ", births, "births")

, in print功能將項(xiàng)目分隔為一個(gè)空格:


>>> print("foo", "bar", "spam")

foo bar spam

或更好地使用字符串格式:


print("If there was a birth every 7 seconds, there would be: {} births".format(births))

字符串格式化功能更強(qiáng)大,它還允許您執(zhí)行其他操作,例如填充,填充,對(duì)齊,寬度,設(shè)置精度等。


>>> print("{:d} {:03d} {:>20f}".format(1, 2, 1.1))

1 002             1.100000

  ^^^

  0's padded to 2

演示:


>>> births = 4

>>> print("If there was a birth every 7 seconds, there would be: ", births, "births")

If there was a birth every 7 seconds, there would be:  4 births


# formatting

>>> print("If there was a birth every 7 seconds, there would be: {} births".format(births))

If there was a birth every 7 seconds, there would be: 4 births


查看完整回答
反對(duì) 回復(fù) 2021-04-02
?
繁星coding

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊

Python是一種非常通用的語(yǔ)言。您可以通過不同的方法打印變量。我列出了以下五種方法。您可以根據(jù)需要使用它們。

例子:

a = 1b = 'ball'

方法1:

print('I have %d %s' % (a, b))

方法2:

print('I have', a, b)

方法3:

print('I have {} {}'.format(a, b))

方法4:

print('I have ' + str(a) + ' ' + b)

方法5:

print(f'I have {a} ')

輸出為:

I have 1 ball


查看完整回答
反對(duì) 回復(fù) 2021-04-02
?
泛舟湖上清波郎朗

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊

還有兩個(gè)


第一個(gè)


>>> births = str(5)

>>> print("there are " + births + " births.")

there are 5 births.

添加字符串時(shí),它們會(huì)串聯(lián)在一起。


第二個(gè)


同樣format,字符串的(Python 2.6和更高版本)方法可能是標(biāo)準(zhǔn)方法:


>>> births = str(5)

>>>

>>> print("there are {} births.".format(births))

there are 5 births.

此format方法也可以與列表一起使用


>>> format_list = ['five', 'three']

>>> # * unpacks the list:

>>> print("there are {} births and {} deaths".format(*format_list))  

there are five births and three deaths

或字典


>>> format_dictionary = {'births': 'five', 'deaths': 'three'}

>>> # ** unpacks the dictionary

>>> print("there are {births} births, and {deaths} deaths".format(**format_dictionary))

there are five births, and three deaths


查看完整回答
反對(duì) 回復(fù) 2021-04-02
  • 4 回答
  • 0 關(guān)注
  • 390 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)