Python Center String Method
In this Python tutorial, we will look at the Python center string method. The center string method allows us to center our strings around a set amount index spots(width) and we can also choose what type of character(fillchar) or symbol fill the space around the string. If the index(width) is less or equal to size of the string then the string itself will be shown. The default character(fillchar) is a space.
Examples Of The Python Center String Method
Example 1:
a = 'This is our String'
a.center(30)
' This is our String '
Example 2:
a.center(30, '-')
'------This is our String------'
Example 3:
a = a.center(50, '+')
a
'++++++++++++++++This is our String++++++++++++++++'
Examples Explained
Example 1:
a = 'This is our String' - We create a new string object and we assign a name(variable) of 'a' to that string obect.
a.center(30)- We call our string object via the variable and call the center string method. With an argument of a 30 index spots.
' This is our String ' - A new string object is returned to us and as you can see the new string object is center in the default spaces Python provided us.
Example 2:
a.center(30, '-')- We use the prior string we call our string object via the variable and then call the center string method on our object. This time, we still use 30 index spots totally, but we change the character to a '-'. Note that the character must be contained in quotes.
'------This is our String------'- We now have a new string object that spans 30 index spots and new index spots that we have created are now represented by hyphens.
Example 3:
a = a.center(50, '+') - This time we call our string object and call the center string method on our string object and reassign the variable a to our new string object. Our new center string method has two arguments span 50 index spaces and replace the default spaces with a +.
'++++++++++++++++This is our String++++++++++++++++'- We get a new string object returned to us. That spans 50 spaces and the empty spaces are now replaced with +.
Conclusion
In this Python tutorial, we have looked at how to use the Python center string method. If you have a questions about this tutorial leave a comment below.
#ng_allrounder
#python_course
#python_tutorials
#cener_method
Nesta página do site você pode assistir ao vídeo on-line Python Center String Method duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário NG Allrounder 01 Janeiro 1970, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 95 vezes e gostou 2 espectadores. Boa visualização!