Creating a String
Strings can be created using single quotes, double quotes, or even triple quotes. Python treats single quotes the same as double quotes.
# creating string
# with single Quotes
String = 'Hello Geek'
print("Creating string with single quotes :", String)
# Creating String
# with double Quotes
String = "yes, I am Geek"
print("Creating String with double quotes :", String)
# Creating String
# with triple Quotes
String = ''' yes, I am Geek'''
print("Creating String with triple quotes :", String)
Output
Creating string with single quotes: Hello Geek
Creating String with double quotes: yes, I am Geek
Creating String with triple quotes yes, I am Geek
String Indexing
Strings are a sequence of characters, which means Python can use indexes to call parts of the sequence. There are two ways of indexing.
- Positive Indexing
- Negative Indexing
# creating a string
String = "GEEK"
# Show first element in string
print("The 1st element is : ", String[0])
# Show 2nd element in string
print("The 2nd element is : ", String[1])
print("The 3rd element is : ", String[2])
print("The 4th element is : ", String[3])
Output
The 1st element is: G
The 2nd element is E
The 3rd element is: E
The 4th element is: K
Negative indexing
# creating a string
String = "GEEK"
# Show last element in string
print("The 4th element is : ", String[-1])
# Show all elements in string
print("The 3rd element is : ", String[-2])
print("The 2nd element is : ", String[-3])
print("The 1th element is : ", String[-4])
Output
The 4th element is : K
The 3rd element is : E
The 2nd element is : E
The 1th element is : G
Top comments (0)