String Center in Python

Level 121 keystrokes

This centers the string 'hi' in a field of width 6.

Python code

# center(width) pads the string with spaces so it sits in the middle of width characters
# "hi" is 2 characters, so 4 spaces are added, 2 on each side: " hi "
1print("hi".center(6))

Output

  hi  
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.

  • "× 2
  • (× 2
  • )× 2
  • .× 1

More Python examples at level 1

← Python typing practice