This article is a part of a challenge series on Python. You can find the link to the previous articles in this series here,
- 30 Days of Python 👨💻 - Day One - Introduction
- 30 Days Of Python 👨💻 - Day 2 - Data Types I
- 30 Days of Python 👨💻 - Day 3 - Data Types II
- 30 Days Of Python 👨💻 - Day 4 - Data Types III
- 30 Days Of Python 👨💻 - Day 5 - Conditions And Loops I
Introduction
Formatted String
String formatting is a neat feature that allows us to dynamically update string content. Suppose we have user information fetched from a server and want to display a custom message based on that. The first idea would be to apply string concatenation something like
- first_name = 'Tom'
- last_name = 'Cruise'
- welcome_message = "Welcome" + " " + first_name + " " + last_name
- print(welcome_message) # Welcome Tom Cruise
If we have more variables, then the dynamic string can be a bit difficult to read. If we have other types of data, then we also need to convert them to string. There is a cleaner approach to this using formatted strings.
- first_name = 'Tom'
- last_name = 'Cruise'
- welcome_message = f'Welcome {first_name} {last_name}'
- print(welcome_message) # Welcome Tom Cruise
The f before the string denotes formatted string. The dynamic values are placed within {}
This is a much cleaner syntax. The JavaScript equivalent is the string interpolation or template strings that were introduced in ES6. It looks like this,
- firstName = 'Tom';
- lastName = 'Cruise';
- welcomeMessage = `Welcome ${firstName} ${lastName}`;
- console.log(welcomeMessage) // Welcome Tom Cruise String Indexes
- language = 'python'
- first_character = language[0]# indexing starts from 0 second_character = language[1] print(first_character)# pprint(second_character)# y# Strings can be manipulated easily with this syntax[start: stop: step - over] range_1 = language[0: 2]# here it starts from index 0 and ends at index 2 range_2 = language[0::1]# starts at 0, stops at end with step over 1 range_3 = language[::2]# starts at 0, till end with step 2 range_4 = language[1: ]# starts at index 1 till end of stringrange_5 = language[-1]# selects last characterrange_6 = language[-2]# second last characterreverse_string = language[::-1]# starts from end and reverses the stringreverse_string_2 = language[::-2]# reverses string and skips 1 characterprint(range_1)# pyprint(range_2)# pythonprint(range_3)# ptoprint(range_4)# ythonprint(range_5)# nprint(range_6)# oprint(reverse_string)# nohtypprint(reverse_string_2)# nhyhttps: //www.digitalocean.com/community/tutorials/how-to-index-and-slice-strings-in-python-3
Immutability
favorite_website = 'dev.to'favorite_website[0] = 'w'print(favorite_website) # TypeError: 'str' object does not support item assignmentBuilt-in string functions and methods
Python has some built-in functions and methods to do operations on string data types. A function is generally an action that can be called independently like print()round() whereas methods are simply functions which are a part of an object and are called with a '.' operator.
- quote = 'javascript is awesome'print(len(quote)) # 21 (len calculates total no of characters)
- new_quote = quote.replace('javascript', 'python')
- print(new_quote) # python is awesome
- capitalize = new_quote.capitalize() print(capitalize) # Python is awesome
- upper_case = new_quote.upper() print(upper_case) # PYTHON IS AWESOME
- print(quote) # javascript is awesome (Note: Strings are immutable!)

Join the conversation! Your thoughts help the community grow.