Python : Mastering strings – part : #1

Hi All,
Today in this article we are going to explore strings in Python. We already have a article on strings but today we have started a advance series of string on Python. So let’s explore strings.
Creating a string
string1 = "Welcome to Knowledge - Junction"
print(string1)
-------------------------------------------------
Output
Welcome to Knowledge - Junction
Creating a string using single quotes
string1 = 'Welcome to Knowledge - Junction'
print(string1)
-----------------------------------------------
Output
Welcome to Knowledge - Junction
Creating a multiline string with triple quotes
- Triple quotes either
"""
or'''
allows string to contain line breaks / multiple lines - Triple quotes can be used for long comments
- In triple quotes we can use special characters like – TAB / NEW LINE
- Triple quotes can be used to represent the string in single quote (”) or in double quote (“”) so we do not need to escape the single quotes or double quotes
string1 = '''Hi! this is
a special multi-line
string!'''
print(string1)
------------------------------------------
Output
Hi! this is
a special multi-line
string!
string2 = ''' "LIFE IS BEAUTIFUL" 'WELCOME TO KNOWLEDGE JUNCTION' '''
print(string2)
------------------------------------------
Output
"LIFE IS BEAUTIFUL" 'WELCOME TO KNOWLEDGE JUNCTION'
Displaying address and object type of the string
string1 = 'Welcome to Knowledge - Junction'
# Displaying address of string
print("ID of string1 is: ",id(string1))
# Displaying object type of a string
print("Object type of string1 is: ",type(string1))
-------------------------------------------------------
Output
ID of string1 is: 1987479891488
Object type of string1 is: <class 'str'>
Concatenation of strings using single quotes, double quotes, and multiline strings
# Concatenation using single quote
string1 = 'India ' + 'is a big ' + 'country'
print(string1)
# Concatenation using double quotes
string2 = "Knowledge" + " - " + "Junction"
print(string2)
# Concatenation using multiline strings
string3 = ('This ' +
'is also a ' +
'a good way to ' +
'concatenate strings!')
print(string3)
----------------------------------------------------
Output
India is a big country
Knowledge - Junction
This is also a a good way to concatenate strings!
Accessing string elements and string slicing
# Accessing elements using enumerate function
string1 = "Knowledge"
for index, letter in enumerate(string1):
print("Letter - ", letter, "index is", index)
# String slicing
# Displaying letters from starting of the string
print("The letters are : ", string1[0:9])
# Displaying letters from starting of the string
print("The letters are : ", string1[::-1])
-----------------------------------------------------
Output
Letter - K index is 0
Letter - n index is 1
Letter - o index is 2
Letter - w index is 3
Letter - l index is 4
Letter - e index is 5
Letter - d index is 6
Letter - g index is 7
Letter - e index is 8
The letters are : Knowledge
The letters are : egdelwonK
Case Conversion Functions
Different functions are their to convert strings into lowercase, uppercase, title case, swap case and capitalized string.
string1 = "PyThoN hAs mAnY LiBrArIeS"
# Capitalized string
print("The capitalized string is - ", string1.capitalize())
# Upper Case String
print("The Upper Case string is - ", string1.upper())
# Lower case string
print("The Lower Case string is - ", string1.lower())
# Title Case string
print("The Title Case string is - ", string1.title())
# Swap Case string
print("The Swap Case string is - ", string1.swapcase())
--------------------------------------------------------------
Output
The capitalized string is - Python has many libraries
The Upper Case string is - PYTHON HAS MANY LIBRARIES
The Lower Case string is - python has many libraries
The Title Case string is - Python Has Many Libraries
The Swap Case string is - pYtHOn HaS MaNy lIbRaRiEs
We have very good series of Python articles, please have a look – https://knowledge-junction.in/category/python/
To make cool games in Python with pygame we also have a series, please have a look – https://knowledge-junction.in/dashboard/python-pygame-series/
To make awesome GUI in Python with tkinter – https://knowledge-junction.in/dashboard/python-gui-programming-with-tkinter-complete-tutorials/
Thank you for reading this article 🙂 and stay tuned for the next articles on strings
Have a nice day!!
Star Ratings
1 Response
[…] Today we are moving one more step forward to master strings in Python. If you didn’t got chance to look at part 1 of mastering strings in Python have a look – https://knowledge-junction.in/2022/12/19/python-mastering-strings-part-1/ […]
You must log in to post a comment.