# 蟒蛇 |將尾隨零添加到字符串

| | | |

`rjust `函數提供一種單行的方式來完成這個特定的任務。因此，它可以很容易地用於我們需要填充的任何行。我們可以指定所需的填充量。

` # Python3演示代碼`
` # 添加前導零 `
` # 使用 rjust () `

` # 初始化字符串 `

` test_string ` ` = ` ` `GFG` `

` `
` # 打印原始字符串 `

` print ` ` (` ` "原始字符串：" ` ` + ` ` str ` ` (test_string)) `

` 需要的零個數 `

` N ` ` = ` ` 4 `

` #我們ing rjust () `
` # 添加前導零 `

` res ` ` = ` ` test_string.rjust (N ` ` + ` ` len ` ` (test_string), ` ` `0` ` `) `

` `
` # 打印結果 `

` print ` ` (` ` "加前導零後的字符串：" ` ` + ` ` str ` ` (res)) `

`原字符串：GFG 加前導零後的字符串：0000GFG `

` # 用於演示的 Python3 代碼 `
` # 添加前導零 `< br>` # 使用 zfill () `

` # 初始化字符串 `

` test_string ` ` = ` ` `GFG` `

` `
` # print原行 `

` print ` ` (` ` "的原始字符串：" ` ` + ` ` str ` ` (test_string)) `

` # Of zeros required `

` N ` ` = ` ` 4 `

` # 使用 zfill () `
` #添加前導零 `

` res ` ` = ` ` test_string。 zfill (N ` ` + ` ` len ` ` (test_string)) `

` # 打印結果 `

` print ` ` (` ` "加前導零後的字符串：" ` ` + ` ` str ` ` (res)) `

`原字符串：GFG 添加後的字符串前導零：0000GFG `

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method