Center Your Text: A Step-by-Step Guide to Python Input Alignment

Learning how to manipulate strings in python

Max N
2 min readJun 1, 2023
Photo by Helio Dilolwa on Unsplash

Introduction

In this tutorial, we’ll walk you through a Python programming problem that involves accepting two input values, aligning the first value in a centered manner, and surrounding it with a custom character. By the end of this post, you’ll have a better understanding of how to work with user inputs, string formatting, and alignment in Python.

Step 1: Accepting User Inputs

First, we need to accept two input values from the user. The input() function in Python allows us to do this. The first input can be either a number or a string, while the second input must be an integer.

value = input("Enter a number or a string: ")
width = int(input("Enter an integer value for alignment: "))

Step 2: Choosing a Custom Character

Next, we need to choose a custom character to surround the first value. You can use any character you prefer, except for the default space character. For this example, we’ll use the asterisk (*) character.

custom_char = "*"

Step 3: Aligning the First Value

--

--

Max N
Max N

Written by Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.

No responses yet