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
- You can use ‘+’ operator to concatenate two strings
String ="String elements can also be accessed using index numbers, just like lists"
print (String[0:7])
#Above print command displays "String " on screen.
print (String[0:7])
#Above print command displays "String " on screen.
TASKS TO DO
# Create a string str1
str1 = “Introduction with strings”
# Now store the length of string str1 in variable str_len
str_len = len(str1)
str_new = “Machine Learning is awesome!”
# Print last eight characters of string str_new (the length of str_new is 28 characters).
print(str_new[20:28])
str2 = “I am doing a course Introduction to Hackathon using ”
str3 = “Python”
# Write a line of code to store concatenated string of str2 and str3 into variable str4
str4 = str2 + str3
Try it yourself in DataCamp here