40923248 cp2020

  • Home
    • Site Map
    • reveal
    • blog
  • 首YA
  • HW1
    • PCH 15 Introduction to Wireless Networking 無線網絡簡介
      • Wireless LANs 無線局域網
      • Wireless Devices 無線設備
      • Wireless Standards 無線標準
      • Privacy and Security 隱私和安全
      • Wireless Networking Types 無線網絡類型
    • PCH 16  Internet Connectivity 互聯網連接
      • Internet Connectivity 互聯網連接
      • Networking  Internet Connectivity  聯網 Internet連接
      • Setting up a Network 建立網絡
  • HW2
  • HW3
    • Birthday Dictionaries 生日字典
      • Exercise 33  and Solution   練習33 和解決方案
      • Discussion topics 討論主題
      • Dictionaries 辭典
      • QUICK REVIEW 快速復審
      • MORE ON DICTIONARY KEYS 有關字典鍵的更多信息
      • String formatting 字符串格式
      • Solutions 解決方案
    • Tic Tac Toe Draw 井字遊戲抽獎
      • Exercise 27 and Solution 練習27 和解決方案
      • Concepts 概念
      • Solutions解決方案
    • Birthday Plots 生日情節
      • Exercise 36 and Solution 練習36和解決方案
      • Discussion 討論區
      • When to make plots 什麼時候作圖
      • Plotting libraries in Python 用Python繪製庫
      • Installing bokeh 安裝背景虛化
      • Using bokeh 使用散景
  • 心得
  • 自評 65 分
MORE ON DICTIONARY KEYS 有關字典鍵的更多信息 << Previous Next >> Solutions 解決方案

String formatting 字符串格式

We’ve talked about strings a lot on this blog:

  • Exercise 1 about printing basic strings 
  • Exercise 6 about turning strings into lists  
  • Exercise 15 about where `join` statements are introduced   

But we want to introduce one more optional concept for this exercise related to string formatting. There are a number of ways to format strings in Python, so I am just going to show you one quick way for a scenario you find yourself in often while programming.

A common scenario is like this: you want to print both a string and a number in the same line using one print() statement. You can solve this problem like so:

我們在此博客上討論了很多字符串:

  • 練習1關於打印基本字符串 
  • 練習6:將字符串轉換為列表  
  • 練習15關於在哪裡引入`join`語句   

但是,我們想為此練習引入與字符串格式化有關的另一個可選概念。有很多方法可以用Python格式化字符串,因此,我將向您展示一種快速編程的方法,以解決您經常在編程時遇到的情況。

常見的情況是這樣的:您想使用一個print()語句在同一行中同時打印字符串和數字。您可以這樣解決此問題:

>>> a = 1
>>> b = 10
>>> print("my number is " + str(a) + " and his number is " + str(b))
my number is 1 and his number is 10

But it gets tedious to use + and str(). Instead, you can use the .format() method to cast (i.e. transform) your number into a string when it gets printed.

但是使用+和會很麻煩str()。相反,你可以使用.format()的方法來投(即變換)你的電話號碼轉換成字符串時,它就會被打印出來。

>>> a = 1
>>> b = 10
>>> print("my number is {} and his number is {}".format(a, b))
my number is 1 and his number is 10

What we are doing is substituting the symbol {} in the print statement in the string we want to display in the exact place we want the number to go, and use the .format() to pass variables to the {} that appear in order. What happens is the variables a and b get converted into strings automatically and injected into our print statement cleanly. You can do this with floats, lists, dictionaries, or anything else you want to display.

There are a number of different formatting options if you want to get specific about how many decimal points to display, etc., but that is out of the scope of this exercise. If you want to read more about string formatting in Python, you can read about it on this helpful website that goes into a great amount of detail.

我們正在做的是將{}要顯示的字符串中的符號替換為打印語句中要顯示的數字的確切位置,然後使用.format()將變量傳遞給{}按順序出現的。發生的事情是變量,a並b自動將其轉換為字符串並print乾淨地註入到我們的語句中。您可以使用浮點數,列表,字典或其他任何想要顯示的內容來執行此操作。

如果要具體說明要顯示的小數點數等,可以使用多種格式設置選項,但這不在本練習的範圍之內。如果您想了解有關Python中字符串格式的更多信息,可以在這個非常有用的網站上詳細了解它。


MORE ON DICTIONARY KEYS 有關字典鍵的更多信息 << Previous Next >> Solutions 解決方案

Copyright © All rights reserved | This template is made with by Colorlib