Python center() - Center a string in Python
The center()
returns the centered string filled with the specified string.
s = 'apple'
s7 = s.center(7, '$')
s8 = s.center(8, '$')
s9 = s.center(9, '$')
s10 = s.center(10, '$')
s11 = s.center(11, '$')
print(s7) # $apple$
print(s8) # $apple$$
print(s9) # $$apple$$
print(s10) # $$apple$$$
print(s11) # $$$apple$$$
The first argument is the length of return string. apple
has 5 letters so center(9, '$')
returns a 9 letter string filled with $
in the left and right.
Python String
- Check if a string contains the other string
- Check if a string starts with another string
- Split a string
- Concatenate strings
- Get the index of substring
- Get the index of the last occurrence of a substring
- Trim a string
- Replace a substring
- Replace multiple spaces with one space
- Fill 0 letters to a string
- Capitalize a string
- Convert a string lowercase
- Check if a string is numeric
- Check if a string is uppercase
- Parse a string to a float
- Format a string
- f-string
- Reverse a string
- Slice a string
- Subtract a string from another string
- Make a random string
- Generate a secure token
- Encode a string
- Get the sha256 value
- Convert an integer to a hexadecimal string
- Get a character or integer value
- Get digits or ASCII letters
- Center a string
- Convert string to tuple