Built-in methods
Learn about built-in methods in Python and Powershell for string manipulation.
Python and PowerShell both provide built-in methods for string manipulations, which are very useful for performing robust string manipulations in programs or in interactive use cases.
The following examples demonstrate some common use cases of string manipulations with built-in methods in PowerShell and Python:
Length of string and finding character at an index
We can find the length of a string object by accessing its 'length' property To find the starting index of a specific character or substring, use the IndexOf() method in PowerShell.
Python provides an inbuilt method called index() that will return the starting index of a character or substring.
Changing the case
To change the case of a string in PowerShell, use ToUpper(). This will change every character of the string to uppercase If we want to convert a string to lowercase, use ToLower, like in the following example.
Python provides the capitalize() inbuilt method in order to change the first character of a string to uppercase, and the title() method to capitalize the first alphabet of every word. To modify each and every character in the string to uppercase, one should use the upper() method.
Split and join strings
While manipulating strings, we will come across scenarios where we are required to split a string into two or more parts by character or word. Alternatively, we may be asked to join two or more characters to form one string. The following are examples demonstrating how to split and join strings in PowerShell using: -Split, split(), and -Join.
Python also offers similar functionality by using the .split() and .join() inbuilt methods.
Reversing a string
To reverse a string in PowerShell, we have to access each character of the string from the index from -1 to the negative of the string’s length value. Then, we’ll perform a join operation to concatenate all characters to form a reversed string.
There is also an alternative, which is to convert string objects to character arrays using the ToCharArray() method. Then, use the array type accelerator’s [array] method named Reverse() to reverse the sequence of characters. Finally, -join() the characters to form a reversed string.
In Python, it is fairly simple to reverse strings by using the reversed() inbuilt method. This method will return the character sequence in reverse. All we need to do is join these characters using the ''.join() method, as in the following example, to form the reversed string:
Removing whitespace
PowerShell has the trim*() methods to perform removal of any white spaces from left, right, or both sides of the string. These are helpful in various scenarios where white spaces are introduced during string manipulations and are not required.
The Python variants of these inbuilt methods are called strip(), lstrip(), and rstrip().
Text alignment
Often, when printing strings on the console, we’ll be required to adjust text alignments. In PowerShell, this is called ‘Padding’. We will pad strings to either left or right like in the following examples.
Similarly, in Python, we adjust text alignment to left, right, or center using the following inbuilt methods.