Question

"center" function Write a function named "center" that receives 2 parameters - "text" (a string) and...

"center" function
Write a function named "center" that receives 2 parameters - "text" (a string) and "width" (an int), and returns a string containing "text" centered in a string of "width" length.
e.g. If you give the function text of "hello" and a width of 11, it should return a string of " hello " (i.e. the text with 3 spaces before and 3 spaces after - a total of 11 characters).
This function replicates Python's built in str.center() method - see the documentation for further information and examples of how it handles things, and don't use this function in your solution...

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

def center(text, width):

    mid = width//2

    mid_text = len(text)//2

    l_padding = mid-mid_text

    r_padding = l_padding

    if(len(text)%2!=0):

        l_padding-=1

    if(width%2!=0):

        l_padding += 1

    return ' '*l_padding + text + ' '*r_padding

result = center('hello', 12)

print(result)

Add a comment
Know the answer?
Add Answer to:
"center" function Write a function named "center" that receives 2 parameters - "text" (a string) and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT