Posts

Python String zfill() Method

The Python zfill() method is used to return a string where zeros (0) are added at the beginning of the string until it reaches a specified length. It returns a new string with the zero-filled version of the original string. For example: text = "Hello world" result = text.zfill(15) print(result) # Output: 00000Hello world You can also use zfill() on a string that contains numbers or symbols. The zfill() method does not affect them and only adds zeros before them. For example: text = "10.00" result = text.zfill(8) print(result) # Output: 00010.00 If you have any questions about this code, you can drop a line in comment .

Python String translate() Method

The Python translate() method is used to return a string where each character is mapped to its corresponding character as per a translation table. It returns a new string with the translated characters. To use translate() , you need to first create a translation table using the maketrans() method. The maketrans() method takes two parameters: a string of characters that need to be replaced, and a string of characters that will replace them. For example: text = "Hello Sam!" # create a translation table that replaces "S" with "P" table = text.maketrans("S", "P") # apply the translation table on the text result = text.translate(table) print(result) # Output: Hello Pam! You can also use translate() to delete certain characters from a string by passing a third parameter to the maketrans() method. The third parameter is a string of characters that will be removed from the original string. For example: text = "Hello Sam!" # crea...

Python String upper() Method

The Python upper() method is used to return a string where all characters are in upper case. It returns a new string with the upper cased version of the original string. For example: text = "Hello my friends" result = text.upper() print(result) # Output: HELLO MY FRIENDS You can also use upper() on a string that contains numbers or symbols. The upper() method ignores them and only converts the alphabetic characters to upper case. For example: text = "heLLo wORld 123 !@#" result = text.upper() print(result) # Output: HELLO WORLD 123 !@# If you have any questions about this code, you can drop a line in comment .  

Python String title() Method

The Python title() method is used to return a string where the first character in every word is upper case, and the remaining characters are lower case. It returns a new string with the title cased version of the original string. For example: text = "hello world" result = text.title() print(result) # Output: Hello World You can also use title() on a string that contains numbers or symbols. The first letter after any number or symbol will be converted to upper case. For example: text = "234 k3l2 *43 fun" result = text.title() print(result) # Output: 234 K3L2 *43 Fun If you have any questions about this code, you can drop a line in comment .

Python String swapcase() Method

The Python swapcase() method is used to convert all uppercase characters to lowercase and vice versa of a given string. It returns a new string with the cases swapped. For example: text = "Hello World" result = text.swapcase() print(result) # Output: hELLO wORLD You can also use swapcase() on a string that contains a mix of upper and lower case characters. All cases will be reversed when swapcase() method is called. For example: text = "HeLLo WoRld" result = text.swapcase() print(result) # Output: hEllO wOrLD If you have any questions about this code, you can drop a line in comment .