Python Assignment 7 String by Hemanth Raj S Civil s01
Python Assignment 7 String by Hemanth Raj S Civil s01
2310064
1. Write a function called rotate_word that takes a string and an integer as Parameters, and returns
a new string that contains the letters from the original String rotated by the given amount.
CODING:
Def rotate_word(text, shift):
Result = ‘’
For char in text:
If char.isalpha():
Shifted = ord(char) + shift
If char.islower():
If shifted > ord(‘z’):
Shifted -= 26
Elif shifted < ord(‘a’):
Shifted += 26
Elif char.isupper():
If shifted > ord(‘Z’):
Shifted -= 26
Elif shifted < ord(‘A’):
Shifted += 26
Result += chr(shifted)
Else:
Result += char
Return result
2. Write a Python program to convert a given string to Snake case. (Hint: use Join()] Snake case is a
variable naming convention where each word is in lower case, and Separated by underscores.
Snake_case refers to the style of writing in which each space is Replaced by an underscore (_)
character, and all letters of each word written in lowercase. It is a commonly used naming
convention in computing, for example for variable and Subroutine names, and for filenames.
Ignore special characters anywhere in the string, And numbers in the beginning of the string.
For example: Date of Birth → date_of_birth This Is In Camel Case → this_is_in_camel_case
123Foo Bar → foo_bar Foo3@ Bar# 23 → foo3_bar_23
CODING:
Import re
Def to_snake_case(input_string)
Words = cleaned_string.split()
Return snake_case
ample usage:
Snake_case_output = to_snake_case(input_string)
Print(snake_case_output)
3. Write a program in python that accepts a string to set up passwords. The program should check
the validity of the password.
CODING:
Import re
Def check_password_validity(password):
# Check length
If len(password) < 8:
Return False
Return False
Return False
Return False
Return True
If check_password_validity(user_password):
Print(“Valid password!”)
Else:
Print(“Invalid password!”)
ADDITIONAL QUESTIONS :
Example: By reversing each letter of REDIVIDER → REDIVIDER Detartrated – an 11-letter word that
the Guinness Book of World Records says is the Longest English palindrome
CODING:
Def is_palindrome(s):
Return s == s[::-1]
Example: Flower, floor -> fl Pot, plot -> p Hot, pot -> no common prefix
CODING:
For I in range(min_len):
If str1[i] == str2[i]:
Prefix += str1[i]
Else:
Break
# Example usage:
# Converting the string to lowercase to handle both uppercase and lowercase characters
String = string.lower()
# Example usage:
Input_string = “This is an example string.”
Vowels_count, consonants_count, spaces_count = count_chars(input_string)
Print(f”Vowels: {vowels_count}”)
Print(f”Consonants: {consonants_count}”)
Print(f”Spaces: {spaces_count}”)
4. Write a menu driven program using inbuilt functions to perform the following:
CODING:
Def substring_occurrence():
Count = string.count(substring)
Def last_occurrence_from_end():
Last_index = string.rfind(substring)
If last_index != -1:
Else:
Print(f”The substring ‘{substring}’ is not found in the string ‘{string}’ from the end.”)
Def right_justify_string():
String = input(“Enter the string to right justify: “)
Justified_string = string.rjust(width)
Def capitalize_first_letter():
Capitalized_string = string.capitalize()
Def check_alphanumeric():
If string.isalnum():
Else:
# Menu
While True:
Print(“\nMenu:”)
Print(“6. Exit”)
Substring_occurrence()
Last_occurrence_from_end()
Right_justify_string()
Capitalize_first_letter()
Check_alphanumeric()
Break
Else: