center() function in python by R4R Team

-center() is a python string function which take two argument one is prefix and suffix and other one is length of required string.

Example-

string is s="mytext"
required string is --mytext--
by
s.center(10,"-")

program-

print("Enter any string")
s=input()
print("Enter prefix or suffix to add")
ps=input()
print("Enter length of required string")
n=int(input())
print("Original string is ",s)
print("Required string is ",s.center(n,ps))
print("String in center covered by 1 is ",s.center(10,'1'))
print("String in center covered by 2 is ",s.center(10,'2'))
print("Original length ",len(s))
print("Length will become ",len(s.center(10,'1')))


output-

Enter any string
mytext
Enter prefix or suffix to add
A
Enter length of required string
16
Original string is mytext
Required string is AAAAAmytextAAAAA
String in center covered by 1 is 11mytext11
String in center covered by 2 is 22mytext22
Original length 6
Length will become 10


-In this program, we take an input of string and prefix character and length then we apply center() function on given string and print them.


Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!