Python string methods with example for searching, replacing and managing cases of chars

preview_player
Показать описание

Managing case of chars
We can manage case of the string by using upper or lower case chars by using string methods. Methods like captalize, casefold, title upper, lower these methods are used to manage cases of the chars inside a string.
Alignment
Various string alignment methods like center, rjust, ljust, restrip, lstring, strip is used to align text in different directions and add filler chars to the string
Searching and replacing string
We can count the number of occurrence of a substring inside a main string by using count method. Similarly we can get the position of the substring inside a main string by suing find or index methods. Main difference between find and index method is index method raises exceptions if the search string is not found, whereas the find method will return -1 if the sub string is not found inside the main string.
We can uses endswith method to check the strings for ending with a substring. Method expandtabs is used to replace space to be used in place of tabs placed inside a string. We can join any iterable object including string by using any substring by using join method.
We can fill a string up to a given length by using zeros
We can do search replace and delete operation by using translate method of string.
Рекомендации по теме