Untitled

 avatar
unknown
plain_text
a year ago
693 B
4
Indexable
#Implement string operation.

name = "Kenil"

print("Name:", name)

#accessing char in string using positive index
print(f"Access character at index 1: {name[1]}.")

#acessing char in string using negative index
print(f"Access character at index -2: {name[-2]}.")

#accessing substring in string
print(f"Accessing substring from 1st to -1st index: {name[1:-1]}.")

#comparing two strings
s1 = "Hello"
s2 = "Hi"
s3 = "Hello"

print(s1 == s2)
print(s1 == s3)

#iterating through string
greet = "Hello world!"

for letter in greet:
    print(letter, end=" ")

print()
#length of string
print(f"Length of string {len(greet)}.")


#Checking for substring
print('a' in greet)
print('hello' in greet)
Editor is loading...
Leave a Comment