0% found this document useful (0 votes)
5 views4 pages

String and Tuple Operations in Python

The document contains various Python programming examples demonstrating string manipulation, tuple operations, and list handling. Key functionalities include counting vowels, consonants, digits, and special characters, checking for palindromes, character frequency, reversing strings, counting words, and performing operations on tuples and lists. Each example is accompanied by code snippets and their corresponding outputs.

Uploaded by

pravash413
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

String and Tuple Operations in Python

The document contains various Python programming examples demonstrating string manipulation, tuple operations, and list handling. Key functionalities include counting vowels, consonants, digits, and special characters, checking for palindromes, character frequency, reversing strings, counting words, and performing operations on tuples and lists. Each example is accompanied by code snippets and their corresponding outputs.

Uploaded by

pravash413
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Count vowels, consonants, digits, and special characters

s = "Hello World! 123"

vowels = consonants = digits = special = 0

for ch in s:

if [Link]() in "aeiou":

vowels += 1

elif [Link]():

consonants += 1

elif [Link]():

digits += 1

else:

special += 1

print("Vowels:", vowels)

print("Consonants:", consonants)

print("Digits:", digits)

print("Special characters:", special)

Output:

Vowels: 3

Consonants: 7

Digits: 3

Special characters: 3

2. Check if a string is palindrome

s = "madam"

if s == s[::-1]:

print("Palindrome")
else:

print("Not Palindrome")

Output:

Palindrome

3. Frequency of each character

s = "banana"

freq = {}

for ch in s:

freq[ch] = [Link](ch, 0) + 1

print(freq)

Output:

{'b': 1, 'a': 3, 'n': 2}

4. Reverse a string without slicing

s = "Python"

rev = ""

for ch in s:

rev = ch + rev

print("Reversed:", rev)

Output:

Reversed: nohtyP

5. Count words in a string

s = "Python is very powerful"

words = [Link]()

print("Number of words:", len(words))

Output:
Number of words: 4

🔹 TUPLE PROGRAMS

6. Find maximum and minimum in a tuple

t = (12, 5, 9, 34, 2)

print("Maximum:", max(t))

print("Minimum:", min(t))

Output:

Maximum: 34

Minimum: 2

7. Count occurrence of element

t = (1, 2, 3, 2, 4, 2, 5)

print("2 occurs", [Link](2), "times")

Output:

2 occurs 3 times

8. Concatenate two tuples

t1 = (1, 2, 3)

t2 = (4, 5, 6)

t3 = t1 + t2

print("Concatenated Tuple:", t3)

Output:

Concatenated Tuple: (1, 2, 3, 4, 5, 6)

🔹 LIST PROGRAMS

9. Find sum of elements in a list

lst = [10, 20, 30, 40]


print("Sum of list:", sum(lst))

Output:

Sum of list: 100

10. Remove duplicates from a list

lst = [1, 2, 2, 3, 4, 4, 5]

unique = []

for x in lst:

if x not in unique:

[Link](x)

print("After removing duplicates:", unique)

Output:

After removing duplicates: [1, 2, 3, 4, 5]

You might also like