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.In 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.
Joining of two or more strings into a single one is called concatenation. Python uses "+" operator for joining one or more strings
In 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.
Python 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.
String count() method returns the number of occurrences of a substring in the given string.
String index() method returns the index of a substring inside the given string.
end(optional) by default its equal to the length of the string.
String upper() convert the given string into Uppercase letters and return new string.
String lower() convert the given string into Lowercase letters and return new string.
String startswith() method returns Boolean TRUE, if the string Starts with the specified substring otherwise, it will return False.
String endswith() method returns Boolean TRUE, if the string Ends with the specified substring otherwise, it will return False.
String split() method break up a string into smaller strings based on a delimiter or character.
Python returned split string as a List
String join() is a string method which returns a string concatenated with the elements of an iterable.
String find() return the index position of the first occurrence of a specified string. It will return -1, if the specified string is not found.
String 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.
String rstrip() returns a copy of the string with trailing characters removed.
String lstrip() returns a copy of the string with leading characters removed.
- Keywords in Python
- Python Operator - Types of Operators in Python
- Python Variables and Data Types
- Python Shallow and deep copy operations
- Python Datatype conversion
- Python Mathematical Function
- Python Substring examples
- How to check if Python string contains another string
- Check if multiple strings exist in another string : Python
- Memory Management in Python
- Python Identity Operators
- What is a None value in Python?
- How to Install a Package in Python using PIP
- How to update/upgrade a package using pip?
- How to Uninstall a Package in Python using PIP
- How to call a system command from Python
- How to use f-string in Python
- Python Decorators (With Simple Examples)
- Python Timestamp Examples