Programming Essentials Python - Predefined Functions - Overview of Strings

Let us get an overview of how strings are used in Python.

  • str is the class or type to represent a string.
  • A string is nothing but a list of characters.
  • Python provides a robust set of functions as part of str to manipulate strings.
  • As a str object is nothing but a list of characters, we can also use standard functions available on top of Python collections such as list, set, etc.

Key Concept 1

Strings in Python can be represented in different ways:

  • Enclosed in single quotes - 'ITVersity"s World'
  • Enclosed in double quotes - "ITVersity's World"
  • Enclosed in triple single quotes - '''ITVersity"s World'''
  • Enclosed in triple double quotes - """ITVersity's World"""

Key Concept 2

If your string itself has a single quote, enclose the whole string in double quotes. If your string itself has a double quote, enclose the whole string in single quotes. Triple single quotes or triple double quotes can be used for multi-line strings.

s = 'Hello World'
type(s)
print(s)
s[:5]
s[-5:]
len(s)
sorted(s)

Hands-On Tasks

Practice the following tasks to apply the concepts discussed in the article:

  1. Create a string variable with your name enclosed in either single or double quotes.
  2. Use string slicing to extract the first three characters from the string and print them.

Conclusion

In this article, we explored the fundamentals of working with strings in Python. Remember to practice these concepts and engage with the community for further learning and support.

Watch the video tutorial here