Sometimes, during the string manipulation, we are into a problem where we need to pad or add leading K to the string as per the requirements. This problem can occur in web development. Having shorthands to solve this problem turns to be handy in many situations. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using
rjust function offers a single line way to perform this particular task. Hence can easily be employed to any string whose padding we need to be done. We can specify the amount of padding required.
The original string : GFG The string after adding leading K : MMMMGFG
Method #2 : Using
String formatting using the format function can be used to perform this task easily, we just mention the number of elements total, element needed to pad, and direction of padding, in this case left.
The original string : GFG The string after adding leading K : 0000GFG
- Python | Add leading Zeros to string
- Python | Remove leading zeros from an IP address
- Python - Remove leading 0 from Strings List
- Python | Remove trailing/leading special characters from strings list
- Python program to read character by character from a file
- Python | Insert character after every character pair
- PyQt5 QSpinBox - Getting the leading value of the font
- Python - Uppercase Nth character
- Python | Character Encoding
- How to capitalize first character of string in Python
- Python | K Character Split String
- Python | Alternate character addition
- Python | Deleting all occurrences of character
- Python - Character coordinates in Matrix
- Python - Lowercase Kth Character in string
- Python | Pair Kth character with each element
- Python - Least Frequent Character in String
- Zip function in Python to change to a new character set
- Python - Groups Strings on Kth character
- Python | Lowercase first character of String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.