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"

Welcome to Knowledge - Junction

Creating a string using single quotes

string1 = 'Welcome to Knowledge - Junction'

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

Hi! this is
a special multi-line



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))

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'

# Concatenation using double quotes
string2 = "Knowledge" + " - " + "Junction"

# Concatenation using multiline strings 
string3 = ('This ' +
'is also a ' +
'a good way to ' +
'concatenate strings!')

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])

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())

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 –

To make cool games in Python with pygame we also have a series, please have a look –

To make awesome GUI in Python with tkinter –

Thank you for reading this article 🙂 and stay tuned for the next articles on strings

Have a nice day!!

Star Ratings

Rating: 5 out of 5.

You may also like...

1 Response

  1. March 19, 2023

    […] 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 – […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: