String Manipulation in Python
Strings are sequences of characters. There are numerous algorithms for processing strings, including for searching, sorting, comparing and transforming. Python strings are "immutable" which means they cannot be changed after they are created . To create a string, put the sequence of characters inside either single quotes, double quotes, or triple quotes and then assign it to a variable.
Access characters in a stringIn order ot access characters from String, use the square brackets [] for slicing along with the index or indices to obtain your characters. Python String index starts from 0.
Python allows negative indexing for its sequences.
The index of -1 refers to the last item, -2 to the second last item and so on.
String ConcatenationJoining of two or more strings into a single one is called concatenation. Python uses "+" operator for joining one or more strings
output
Reverse a StringIn Python Strings are sliceable. Slicing a string gives you a new string from one point in the string, backwards or forwards, to another point, by given increments. They take slice notation or a slice object in a subscript:
The subscript creates a slice by including a colon within the braces:
It works by doing [begin:end:step] - by leaving begin and end off and specifying a step of -1, it reverses a string.
output
String MethodsPython has several built-in methods associated with the string data type. These methods let us easily modify and manipulate strings. Built-in methods are those that are defined in the Python programming language and are readily available for us to use. Here are some of the most common string methods.
Python String len() methodString len() method return the length of the string.
output
Python String count() methodString count() method returns the number of occurrences of a substring in the given string.
output
Python String index() methodString index() method returns the index of a substring inside the given string.
end(optional) by default its equal to the length of the string.
output
Python String upper() methodString upper() convert the given string into Uppercase letters and return new string.
output
Python String lower() methodString lower() convert the given string into Lowercase letters and return new string.
output
Python String startswith() methodString startswith() method returns Boolean TRUE, if the string Starts with the specified substring otherwise, it will return False.
output
Python String endswith() methodString endswith() method returns Boolean TRUE, if the string Ends with the specified substring otherwise, it will return False.
output
Python String split() methodString split() method break up a string into smaller strings based on a delimiter or character.
output
example
output
example
output
Python returned split string as a List
output
Python String join() methodString join() is a string method which returns a string concatenated with the elements of an iterable.
output
Python String find() methodString find() return the index position of the first occurrence of a specified string. It will return -1, if the specified string is not found.
output
Python String strip() methodString strip() remove the specified characters from both Right hand side and Left hand side of a string (By default, White spaces) and returns the new string.
output
Python String rstrip() methodString rstrip() returns a copy of the string with trailing characters removed.
output
Python String lstrip() methodString lstrip() returns a copy of the string with leading characters removed.