Strings in Python


Code Example 1

fruit = 'banana'
x=fruit[3]
print(x)

Output

a

Len Function

Code Example1

fruit = 'banana'
x=len(fruit)
print(x)

Output

6

Code Example 2

fruit = 'banana'
index = 0
while index < len(fruit):
    x = fruit[index]
    print(index,x)
    index = index + 1

Output

0 b
1 a
2 n
3 a
4 n
5 a

Code Example 3

fruit = 'banana'
count = 0
for letter in fruit:
    if letter == 'a':
        count = count + 1
print (count)

Output

3

Because there’re 3 a in banana


Question

  • What will the following code print?:

      for n in "banana":
          print(n)
    
  • Ans

      b
      a
      n
      a
      n
      a
    

Notes mentioning this note


ນີ້ຄືກຣາຟທີ່ລິ້ງໄປຫາທຸກໜ້າໃນ garden ນີ້ 🕸️.