Python String Concept

String in Python

A string is a collection of characters stored in a sequence using indexing. String indexing starts from 0 to length-1. Strings can be declared using:

a = 'welcome'          # single line string
a = "welcome"          # single line
a = """ welcome in scs """    # multi-line string

WAP to Count Total Vowels and Consonants

s = 'welcome'
c = 0       # vowel count
d = 0       # consonant count

for i in range(0, len(s)):
    if s[i] in ('a','e','i','o','u'):
        c += 1
    else:
        d += 1

print("vowel", c, "consonant", d)

Another Method

s = input("Enter word")
v = 0
c = 0

for i in range(len(s)):
    if s[i] in ('a','e','i','o','u'):
        v += 1
    else:
        c += 1

print("Total vowel =", v, "Consonant =", c)

Another Method (Using for-each)

s = input("Enter word")
v = 0
c = 0

for ch in s:
    if ch in ('a','e','i','o','u'):
        v += 1
    else:
        c += 1

print("Total vowel =", v, "Consonant =", c)

ASCII and Strings

Most string programs use ASCII. Python provides:

1) ord() → char to ASCII

print(ord('a'))   # 97

2) chr() → ASCII to char

print(chr(65))    # 'A'

WAP to Find Max Character in String

s = 'ramesh'
m = s[0]

for i in range(1, len(s)):
    if m < s[i]:
        m = s[i]

print(m)

ASSIGNMENT PROGRAMS

1) Reverse String but Keep Vowels in Same Position

Example:
Ramesh → hasemr
s = "ramesh"
s1 = ""

s1 += s[5]
s1 += s[1]
s1 += s[4]
s1 += s[3]
s1 += s[2]
s1 += s[0]

print(s1)

2) WAP to Check Palindrome

s = "madam"
c = len(s) - 1
flag = True

for i in range(len(s)//2):
    if s[i] != s[c-i]:
        flag = False
        break

if flag:
    print("palindrome")
else:
    print("not palindrome")

Another Method

s = "gggggg"

for i in range(len(s)//2):
    if s[i] != s[len(s)-1-i]:
        print("Not Palindrome")
        break
else:
    print("Palindrome")

3) Convert Uppercase → Lowercase and Lowercase → Uppercase

s = "MADAM"
s1 = ""

for i in range(len(s)):
    s1 += chr(ord(s[i]) + 32)

print(s1)

Program to Convert String into Opposite Case

s = "Hello"
s1 = ""

for d in s:
    if ord(d) >= 65 and ord(d) <= 90:
        s1 += chr(ord(d) + 32)     # upper → lower
    else:
        s1 += chr(ord(d) - 32)     # lower → upper

print(s1)

4) Replace Each Character by Next Consecutive Character

Example: Manish → nbojti (You may implement this as a student assignment)

5) Extract Numbers, Alphabets, Special Characters Separately

s = "abcd123@7$f"
alpha = ""
num = ""
spec = ""

for ch in s:
    if ch.isalpha():
        alpha += ch
    elif ch.isdigit():
        num += ch
    else:
        spec += ch

print(alpha)
print(num)
print(spec)

6) WAP to Count Repeated Characters

Example: hello → h1 e1 l2 o1
s = "mangalam"

for i in range(len(s)):
    c = 0
    for k in range(i):
        if s[i] == s[k]:
            break
    else:
        for j in range(i, len(s)):
            if s[i] == s[j]:
                c += 1
        print(s[i], c)

7) WAP to Validate Email

Conditions:
  • Must contain @
  • Must contain .
  • Position of @ must be before last dot

8) WAP to Check Password Strength

Strong Password:
  • At least 3 special characters
  • Not consecutive
  • Must not contain first name / last name
  • At least 1 uppercase
  • At least 1 numeric
  • Minimum length = 6
Medium Password:
  • At least 2 special characters
  • Minimum length = 6