1 of 14

COL100 �Introduction to Computer Science

Lecture 17 – Strings

February, 2025

Semester II

2024-2025

Vireshwar Kumar, Aaditeshwar Seth

CSE@IITD

2 of 14

Strings

  • String is a sequence of characters (case sensitive)

​

  • Letters, special characters, spaces, digits

​

  • Enclose in quotation marks or single quotes

​

​

hi = "hello there"

​

2

3 of 14

Concatenate Strings

​

​

hi = "hello there"

name = "ana"

greeting = hi + " " + name

# “hello there ana "

​

3

4 of 14

Type Casting Strings

stringNum = “5“

print(type(stringNum))

intNum = int(stringNum)

print(type(intNum))

4

5 of 14

String as an Array of Characters

  • Inbuilt function len() gives the length of the specified string

fruit = “banana”

print(len(fruit)) #6

5

6 of 14

Forward Indexing

  • Strings are indexed. Square brackets are used to perform indexing into a string to get the value at certain index/position
  • Example: fruit=“banana”
    • fruit[0] -> “b”
    • fruit[1] -> “a”
    • fruit[2] -> “n”
    • fruit[3] -> “a”
    • fruit[4] -> “n”
    • fruit[5] -> “a”

​

​

  • This indexing process is called “Forward" Indexing

6

7 of 14

Backward Indexing

  • We can negative integers to perform “Reverse" Indexing.
  • Last character of the string can be obtained using the index -1, which refers to (n-1)th element.

​

  • Example: fruit=“banana”
    • fruit[-1] -> “a”
    • fruit[-2] -> “n”
    • fruit[-3] -> “a”
    • fruit[-4] -> “n”
    • fruit[-5] -> “a”
    • fruit[-6] -> “b”

7

8 of 14

Looping through String

fruit = “banana”

for i in range(0,len(fruit)):

print (fruit[i])

​

fruit = “banana”

for letter in fruit:

print (letter)

​

first = “krsd”

last=“ing”

for letter in first:

print (letter+last)

8

9 of 14

Slicing Strings

  • Slicing extracts from a string by specifying indices: [start, stop, step]

​

  • If step is not specified, [start, stop] then a value of 1 is assumed for step (i.e., step = 1 as default).

fruit = “watermelon”

print(fruit[:5]) # “water”

print(fruit[5:]) # “melon”

​

s = “abcdefgh”

print(s[3:6]) # “def”

print(s[3:6:2]) # “df”

print(s[::]) # “abcdefgh”

print(s[::-1]) # “hgfedcba”

​

9

10 of 14

Methods for Strings

  • Conversion to uppercase (capital) letters

​

  • Checks if the letter is lower case or not

s = “abcdefgh”

capital_s = s.upper()

print(capital_s)

​

​

fruit = "Watermelon"

if fruit[0].islower():

print (“The first letter is not capital”)

else:

print (“The first letter is capital”)

​

10

11 of 14

Find Method

  • Finding a letter in a string using the find() method.
  • Output index is found at forward index.
  • -1 (is not found)

​

fruit = ”watermelon"

print(fruit.find(“m”))

print(fruit.find(“g”))

print(fruit.find(“e”,4))

11

12 of 14

String Comparions

  • Two strings can be compared using comparison operators

​

s1 = “acol100”

S2 = “acol100”

print(S1 == s1)

​

word1 = “hello”

word2 = “yellow”

if (word1 < word2):

print (word1 + ’ comes before ‘ + word2)

else:

print (word2 + ‘ comes before ‘ + word1)

​

12

13 of 14

Strings are Immutable

  • Strings are "immutable" i.e., they can not be directly assigned, or we cannot assign a character to an index.

​

  • But a string can be sliced and then concatenated.

​

​

word1 = “hello”

word1[0]=‘y’ # error

​

word2 = word1[1:len(word1)]

word2 = ‘y’+word2

13

14 of 14

Additional examples

  • Check for palindrome using loops
  • Look up various inbuilt string functions and see how you can write them yourselves

​

14