Be sure to like, share and comment to show your support for our tutorials.
=======================================
Channel - https://goo.gl/pnKLqE
Playlist For This Tutorial - https://goo.gl/EyZFti
Latest Video - https://goo.gl/atWRkF
Facebook - / mastercodeonline
Twitter - https://twitter.com/mastercodeonlin?l...
Website - http://mastercode.online
======================================
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.
En esta página del sitio puede ver el video en línea Python Center String Method de Duración hora minuto segunda en buena calidad , que subió el usuario Code master 23 septiembre 2015, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 12,893 veces y le gustó 134 a los espectadores. Disfruta viendo!