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.

Press + to interact
## built-in string methods
'powershell'.Length # length of string
'powershell'.IndexOf('s') # find characters at an index

Python provides an inbuilt method called index() that will return the starting index of a character or substring.

Press + to interact
## built-in string methods
print(len('python')) ## length of string
print('python'.index('o')) # find characters at an index

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.

Press + to interact
## capitalize all words or characters
'hello world'.ToUpper()
## capitalize first alphabet of each word
'HELLO WORLD'.ToLower()

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.

Press + to interact
## capitalize 1st char of the string
print('hello world'.capitalize())
## capitalize 1st alphabet of each word to upper case
print('hello world'.title())
## capitalize all words or characters
print('hello world'.upper())

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.

Press + to interact
## splitting a string .split() -split
$string = "My name is Prateek Singh `nI` 'm from India"
$string.split(' ') # splitting the string by white space
$string -split 'prateek' # splitting the string by a word
$string -split '\n' # splitting by newline character
## joining string .join() -join
[char[]]'Prateek' -join ' ' # Joining each char with a space
[char[]]'Prateek' -join '*' # Joining each char with a '*'

Python also offers similar functionality by using the .split() and .join() inbuilt methods.

Press + to interact
string="My name is Prateek Singh \n I'm from India"
## splitting a string .split()
print(string.split(' ')) # splitting the string by white space
print(string.split('is')) # splitting the string by a word, eq. 'is'
print(string.split('\n')) # splitting by newline character
## joining string .join()
print(' '.join('Prateek')) # Joining each char with a space
print('*'.join('Prateek')) # Joining each char with a '*'

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.

Press + to interact
$s = 'powershell'
$s[-1..-($s.Length)] -join ''
$r = $s.ToCharArray() ; [array]::Reverse($r) ; -join($r)

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:

Press + to interact
## reversing a string reversed()
print(''.join(reversed('python'))) # reversing a string
### alternatively
print(''.join(list('python')[::-1])) # reversing a 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.

Press + to interact
## stripping white spaces from both ends of the string
' Hey there '.trim()
### stripping white spaces from the left end of the string
' Hey there '.trimstart()
### stripping white spaces from the right end of the string
' Hey there '.trimend()

The Python variants of these inbuilt methods are called strip(), lstrip(), and rstrip().

Press + to interact
## stripping white spaces from both ends of the string
print(' Hey there '.strip())
## stripping white spaces from left end of the string
print(' Hey there '.lstrip())
## stripping white spaces from right end of the string
print(' Hey there '.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.

Press + to interact
##string padding .PadLeft() .PadRight()
'Hello'.PadLeft(30)
'Hello'.PadRight(30, '-')
'Hello'.PadLeft(30, '*')

Similarly, in Python, we adjust text alignment to left, right, or center using the following inbuilt methods.

Press + to interact
## string adjustment .rjust() .ljust(), .center()
print('Hello'.rjust(30))
print('Hello'.ljust(30, '-'))
print('Hello'.rjust(30, '*'))
print('Hello'.center(30, '_'))