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

String Manipulation P

This document contains 15 code snippets demonstrating various string manipulation techniques in Python, including calculating string length, summing digits in a string, capitalizing words, counting vowels, swapping punctuation, lowercase initial characters, converting to a list, reversing a string, counting substrings, and formatting numbers as percentages.

Uploaded by

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

String Manipulation P

This document contains 15 code snippets demonstrating various string manipulation techniques in Python, including calculating string length, summing digits in a string, capitalizing words, counting vowels, swapping punctuation, lowercase initial characters, converting to a list, reversing a string, counting substrings, and formatting numbers as percentages.

Uploaded by

Sashwath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

STRING MANIPULATION/TEXT HANDLING

PROGRAMS

#to calculate the length of a string


str1='COMPUTER SCIENCE OR INFORMATICS
PRACTICES'
count = 0
for char in str1:
count += 1
print(count)

#to compute sum of digits of a given string


str1='JK23 KSD 315 SD990'
sum = 0
for x in str1:
if x.isdigit() == True:
z = int(x)
sum = sum + z
print(sum)
#to capitalize first and last letters of each word of a
given string
str1='computer science and informatics practices'
str1 = result = str1.title()
result = ""
for word in str1.split():
result += word[:-1] + word[-1].upper() + " "
print(result)

#to count and display the vowels of a given text


text='computer science and informatics practices'
vowels='aeiuoAEIOU'
l=len([letter for letter in text if letter in vowels])
str=[letter for letter in text if letter in vowels]
print(l)
print(str)
#to swap comma and dot in a string
amount = "125.35,25"
maketrans = amount.maketrans
amount = amount.translate(maketrans(',.', '.,'))
print(amount)
#to lowercase first n characters in a string
str1 = 'COMPUTER SCIENCE'
str=str1[:8].lower() + str1[8:]
print(str)

#to convert a string in a list


str1 = "computer science and informatics practices"
s=str1.split(' ')
print(s)

#to reverse a string


str1 = "computer science and informatics practices"
s=''.join(reversed(str1))
print(s)

#to count occurrences of a substring in a string


str1 = "computer science and informatics practices"
n=str1.count("science")
print(n)
#to format a number with a percentage
x = 0.75
print("\nOriginal Number: ", x)
print("Formatted Number with percentage:
"+"{:.2%}".format(x));

#to check string is palindrome or not


s=input("enter string")
rev = s[::-1]
if (s == rev):
print("string is palindrom")
else:
print("string is not palindrom")

You might also like