1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to Python & Machine Learning (with Analytics Vidhya Hackathons)

Exercise

Create a String

Strings can simply be defined by use of single ( ‘ ), double ( ” ) or triple ( ”’ ) inverted commas. Strings enclosed in triple quotes ( ”’ ) can span over multiple lines. A few things to keep in mind about strings:

  • Strings are immutable in Python, so you can not change the content of a string.
  • Function len() can be used to get length of a string
  • You can access the elements using indexes as you do for lists
String ="String elements can also be accessed using index numbers, just like lists"

print (String[0:7])

#Above print command displays "String " on screen.
  • You can use '+' operator to concatenate two strings

Instructions

100 XP
  • Use the len() function to store the length of string
  • Use start and end index to access the required characters, e.g. str[0:3] to return first three characters of string str
  • '+' operator is used to concatenate (combine) two strings