String Center in Python

Level 242 keystrokes

This centers a string within a specified width. The center() method pads with the given character.

Python code

# Create a 5-character string
1text = "hello"
# center(11, "*") pads the text with "*" to a total width of 11
# 11 - 5 = 6 fill characters, 3 on each side: prints ***hello***
2print(text.center(11, "*"))

Output

***hello***
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • "× 4
  • (× 2
  • )× 2
  • ,× 1
  • .× 1
  • *× 1
  • =× 1

More Python examples at level 2

← Python typing practice