Python – String Operations
In previous article, we saw about What is String in Python?. In this article we can see String operations in Python. Python provides some operations or functions that are available or built-in to use with strings.
Each below topics are explained for better understanding and to use it wisely for our purpose,
-
- String Concatenation
- Accessing a Character
- Accessing a Subset of Characters
- Repeating Strings
- Splitting Strings
- Counting Strings
- Replacing Strings
- Finding Sub Strings
- Converting Other Types into Strings
- Comparing Strings
- Other String Operations
- String Formatting
- String Templates
- Function/Method Names
String Concatenation
‘+’ operator is used to concatenate two string operations in python. That is you can take one string and add it to another string to create a new third string:
string_1 = 'Hello' string_2 = " World" string_3 = string_1 + string_2 print(string_3) print('Welcome' + 'TalksInfo')
The output from this is:
Hello World Welcome TalksInfo
Accessing a Character
As a string is a fixed sequence of letters, we can use square brackets and an index position to retrieve a specific character from within a string. Also, Strings are indexed from Zero which means the first character is in position 0, the second in position 1 etc. For example,
my_string = 'Hello World' print(my_string[4])
Thus in above my_string[4] indicates that we want to obtain the fifth character in the string, which in this case is the letter ‘o’.
Accessing a Subset of Characters
It is also possible to obtain a subset of the original string, often referred to as a substring. This string operation on python can be done using the square brackets notation but using a ‘:’ operator to indicate the start and end points of the sub string. If one of the positions is omitted then the start or end of the string is assumed to be from first or last respectively. For example,
my_string = 'Hello World' print(my_string[4]) # characters at position 4 print(my_string[1:5]) # from position 1 to 5 print(my_string[:5]) # from start to position 5 print(my_string[2:]) # from position 2 to the end
Above code will give output as below,
o ello Hello llo World
Repeating Strings
We can also use the ‘*’ operator with strings. In the case of strings this means repeat the given string a certain number of times. This generates a new string containing the original string repeated n number of times. For example:
print('*' * 10) print('Hi' * 10)
Output as below,
********** HiHiHiHiHiHiHiHiHiHi
Splitting Strings
split() function is used to separate/split string based on a specific character such as a space or a comma. For example,
title = 'The Good, The Bad, and the Ugly' print('Source string:', title) print('Split using a space') print(title.split(' ')) print('Split using a comma') print(title.split(','))
Above code produces output as below,
Source string: The Good, The Bad, and the Ugly Split using a space ['The', 'Good,', 'The', 'Bad,', 'and', 'the', 'Ugly'] Split using a comma ['The Good', ' The Bad', ' and the Ugly']
Counting Strings
It is possible to find out how many times a string is repeated in another string. This is done using the count() operation for example
my_string = 'Count, the number of spaces' print("my_string.count(' '):", my_string.count(' '))
Above code provides output as,
my_string.count(' '): 8
*** indicating that there are 8 spaces in the original string.
Replacing Strings
One string can replace a substring in another string. This is done using the replace() method on a string. For example:
welcome_message = 'Hello World!' print(welcome_message.replace("World", "TalksInfo"))
code produces output as below,
Hello TalksInfo!
Finding Sub Strings
You can find out if one string is a substring of another string using the find() method. This method takes a second string as a parameter and checks whether the second string is available in original string. For example:
print('TalksInfo welcomes everyone!'.find('TalksInfo')) print('TalksInfo says goodbye!'.find('welcome'))
Above code produces output as below,
0 -1
where as ZERO indicates the index position of TalksInfo in original string, and -1 indicates string ‘welcome’ is unavailable in the original string.
Converting Other Types into Strings
If you try to use the ‘+’ concatenation operator with a string and some other type such as a number then you will get an error. For example if you try the following:
msg = 'TalksInfo welcomes year ' + 2020 print(msg)
Above code will give error as below,
TypeError: must be str, not int
To concatenate integer with string, you must convert integer to a string. This can be done using the str() function. For example,
msg = 'TalksInfo welcomes year ' + str(2020) print(msg)
Above code snippet will give output as,
TalksInfo welcomes year 2020
Comparing Strings
To compare one string with another you can use the ‘==’ equality and ‘!=’ not equals operators. These will compare two strings and return either True or False indicating whether the strings are equal or not.
For example:
print('Java' == 'Java') # prints True print('Java' == 'Python') # prints False print('Java' != 'Python') # prints True
You should note that strings in Python are case sensitive thus the string ‘James’ does not equal the string ‘james’. Thus:
print('Java' == 'java') # prints False
Other String Operations in python
Also, we have numerous string operations in Python. Note all of these operations use the dot notation in the operation.
some_string = 'Hello World' print('Testing a String') print('-' * 20) print('some_string', some_string) print("some_string.startswith('H')", some_string.startswith('H')) print("some_string.startswith('h')", some_string.startswith('h')) print("some_string.endswith('d')", some_string.endswith('d')) print('some_string.istitle()', some_string.istitle()) print('some_string.isupper()', some_string.isupper()) print('some_string.islower()', some_string.islower()) print('some_string.isalpha()', some_string.isalpha()) print('String conversions') print('-' * 20) print('some_string.upper()', some_string.upper()) print('some_string.lower()', some_string.lower()) print('some_string.title()', some_string.title()) print('some_string.swapcase()', some_string.swapcase()) print('String leading, trailing spaces', " xyz ".strip())
Above code snippet provides below output,
Testing a String -------------------- some_string Hello World some_string.startswith('H') True some_string.startswith('h') False some_string.endswith('d') True some_string.istitle() True some_string.isupper() False some_string.islower() False some_string.isalpha() False String conversions -------------------- some_string.upper() HELLO WORLD some_string.lower() hello world some_string.title() Hello World some_string.swapcase() hELLO wORLD String leading, trailing spaces xyz
String Formatting
Python provides a sophisticated formatting system for strings that can be useful for printing information out or logging information from a program. The string formatting system uses a special string known as the format string that acts as a pattern defining how the final string will be laid out.
This format string can contain placeholders {} that will be replaced with actual values when the final string is created. A set of values can be applied to the format string to fill the placeholders using the format() method. For example,
format_string = 'Hello {}!' print(format_string.format('TalksInfo')) # Allows multiple values to populate the string with placeholders name = "TalksInfo" age = 2 print("{} is {} years old".format(name, age)) # Can specify an index for the substitution format_string = "Hello {1} {0}, you got {2}%" print(format_string.format('Trump', 'Melania', 75)) # Can use named substitutions, order is not significant, key=value pair format_string = "{artist} sang {song} in {year}" print(format_string.format(artist='Paloma Faith', song='Guilty', year=2017)) #colon (':') followed by the width to use. print('|{:25}|'.format('25 characters width')) #also indicate an alignment using <, >, ^ print('|{:<25}|'.format('left aligned')) # The default print('|{:>25}|'.format('right aligned')) print('|{:^25}|'.format('centered')) # Can format numbers with comma as thousands separator print('{:,}'.format(1234567890)) print('{:,}'.format(1234567890.0))
Above code snippet will give output as below,
Hello TalksInfo! TalksInfo is 2 years old Hello Melania Trump, you got 75% Paloma Faith sang Guilty in 2017 |25 characters width | |left aligned | | right aligned| | centered | 1,234,567,890 1,234,567,890.0
String Templates
An alternative to using string formatting is to use string Templates. A string template is a class (type of thing) that is created via the string.
Template() function. The template contains one or more named variables preceded with a $ symbol. The Template can then be used with a set of values that replace the template variables with actual values.
import string # Initialise the template with ¢variables that # will be substitute with actual values template = string.Template('$artist sang $song in $year') print(template.substitute(artist='Freddie Mercury', song='The Great Pretender', year=1987))
Note that it is necessary to include an import statement at the start of the program as Templates are not provided by default in Python; they must be loaded from a library of additional string features.
The actual values can be substituted into the template using the substitute() function. The substitute function takes a set of key=value pairs, in which the key is the name of the template variable (minus the leading $ character) and the value is the value to use in the string.
Above code snippet will produce output as below,
'Freddie Mercury sang The Great Pretender in 1987'
Also you can create what is known as a dictionary using dict() method. A dictionary is a structure comprised of key:value pairs in which the key is unique. This allows a data structure to be created containing the values to use and then applied to the substitute function. For example,
d = dict(artist = 'Billy Idol', song='Eyes Without a Face', year = 1984) print(template.substitute(d))
Will produce output as below,
Billy Idol sang Eyes Without a Face in 1984
Function/Method Names
Be very careful with capitalisation of function/method names; in Python isupper() is a completely different operation to isUpper(). If you use the wrong case Python will not be able to find the required function or method and will generate an error message. Do not worry about the terminology regarding functions and methods at this point—for now they can be treated as the same thing and only differ in the way that they recalled or invoked.
Conclusion
In this article, we went through various string operations using Python. Still more functions are available in python. Even this link can give more information on string operations in python.