Python for Data Science: Chapter 2: Functions and Files

Python Programming examples based on string

Python for Data Science: Python Programming examples based on string

Python Programming examples based on string

 

Example:2

Write a Python program to find the length of a string.

Solution:

def str_length(str):

len=0

for ch in str:

    len+=1

    return len

print("\t\t Program to find the length of a string")

print("Enter some string: ")

str=input()

print("Length of a string is: ",str_length(str))

Output

Program to find the length of a string

Enter some string:

python

Length of a string is: 6

 

Example:3

Write a Python program to count occurrences of each word in given sentence.

Solution:

def count_occur(str):

    data=dict()

    words=str.split()

    for word in words:

        if word in data:

            data[word]+=1

        else:

            data[word]=1

        return data

print("Enter some string: ")

str=input()

print(count_occur(str))

Output

Enter some string :

A big black bear sat on a big black rug

{'A': 1, 'big': 2, 'black': 2, 'bear': 1, 'sat': 1, 'on': 1, 'a': 1, 'rug': 1}

 

Example:4

Write a Python program to copy one string to another.

Solution :

print("Enter some string: ")

str1=input()

str2="

for i in range (len(str1)):

    str2=str2+str1[i]

print("The copied string is: ",str2)

Output

Enter some string:

Technical

The copied string is: Technical

 

Example:5

Write a Python program to check if a substring is present in the given string or not.

Solution :

print("Enter some string: ")

str1=input()

print("Enter a word: ")

str2=input()

if(str1.find(str2)= = ‒1):

        print("The substring ",str2," is not present in ",str1)

else:

        print("The substring ",str2," is present in ",str1)

Output

Enter some string:

sky blue

Enter a word:

blue

The substring blue is present in sky blue

 

Example:6

Write a Python program to count number of digits and letters in a string.

Solution :

print("Enter some string: ")

str=input()

digit_count = 0

letter_count = 0

for i in str:

    if(i.isdigit()):

    digit_count+=1

else:

    letter count+=1

print("Total number of digits in ",str," are ",digit_count)

print("Total number of letters in ",str," are ",letter_count)

Output

Enter some string :

Python123Program

Total number of digits in Python123Program are 3

Total number of letters in Python123Program are 13

 

Example:7

Write a Python program to count number of vowels in a string.

Solution :

print("Enter some string: ")

str=input()

vowel_count=0

for i in str:

if((i= ='a' or i= ='e' or i= ='i' or i= ='o' or i= ='u')

or((i= ='A' or i= ='E' or i= ='I' or i= ='0' or i= ='U'))):

    vowel_count + =1

print("Total number of vowels in ",str," are ",vowel_count)

Output

Enter some string:

India

Total number of vowels in India are 3

 

Example:8

Write a Python program to check if the string is palindrome or not.

Solution :

print("Enter some string: ")

str=input()

rev_str=reversed(str)

if(list(str)==list(rev_str)):

    print("The string ",str," is palindrome")

else:

    print("The string ",str," is not palindrome")

Output

Enter some string :

madam

The string madam is palindrome

 

Example:9

Write a Python program to sort the word in a sentence in an alphabetic order.

Solution :

print("Enter some string: ")

str=input()

words_list=str.split()

words_list.sort()

print("The words in sorted order are...")

for word in words_list:

    print(word)

Output

Enter some string :

I like python program very much

The words in sorted order are...

I

like

much

program

python

very

 

Python for Data Science: Chapter 2: Functions and Files : Tag: Computer Programming, Python, Data Science : - Python Programming examples based on string


Python for Data Science: Chapter 2: Functions and Files



Under Subject


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science - Laboratory

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation