Class 12 Programs
- 1.Read text file line by line with # separator
- 2.Count vowels, consonants, uppercase, lowercase in file
- 3.Remove lines containing character 'a'
- 4.Binary file with name and roll number search
- 5.Binary file update marks by roll number
- 6.Random number generator (Dice Simulator)
- 7.Stack implementation using list
- 8.CSV file with user-id and password
- 9.User Defined Functions to manipulate List Store Indices of Non Zero elements &Double the Odd values
- 10.WORKING WITH BINARY FILE IN PYTHON Create a binary file, Search and display the records from the binary file
- 11.TEXT FILES IN PYTHON Remove duplicate lines from the file & Display unique words present in the file
- 12.TEXT FILES IN PYTHON Copying lines to a new file & Replacing the '-' sign with a blankspace
- 13.TEXT FILES IN PYTHON Count the lines starts with I/T & Display the lines with exactly 6 words
- 14.TEXT FILES IN PYTHON Count the occurrences of word & Count number of vowels and consonants
- 15.User Defined Functions to Manipulate List Find the Longest Word and Shifting the elements to left
- 16.IMPLEMENTATION OF STACK USING LIST IN PYTHON
- 17.ALTER table to add new attributes / modify data type / drop attribute
- 18.UPDATE table to modify data
- 19.ORDER By to display data in ascending / descending order
- 20.DELETE to remove tuple(s)
- 21.GROUP BY and find the min, max, sum, count and average
- 22.INTERFACING PYTHON WITH MYSQL DATABASE CONNECTIVITY APPLICATION PROGRAM - DELETE RECORDS
- 23.INTERFACING PYTHON WITH MYSQL DATABASE CONNECTIVITY APPLICATION PROGRAM - UPDATE RECORDS
- 24.INTERFACING PYTHON WITH MYSQL DATABASE CONNECTIVITY APPLICATION PROGRAM - INSERT RECORDS
Quick Tips
- • Import random module for number generation
- • Use randint(1, 6) for dice simulation
- • Handle user input validation properly
Longest Word Shifting Left
AIM
To write a Python program to shift the longest word in a sentence to the left.
ALGORITHM
- Start
- Input a sentence
- Split the sentence into words
- Find the longest word
- Shift the longest word to the left
- Output the modified sentence
- Stop
PROGRAM
# Python program to shift the longest word in a sentence to the left
def shift_longest_word_left(sentence):
words = sentence.split()
longest_word = max(words, key=len)
longest_word_index = words.index(longest_word)
words[longest_word_index] = longest_word[1:] + longest_word[0]
return ' '.join(words)
sentence = input("Enter a sentence: ")
modified_sentence = shift_longest_word_left(sentence)
print("Modified sentence:", modified_sentence)
OUTPUT
Enter a sentence: Hello world from Python
Modified sentence: Hello wolrd from Python
CONCLUSION
Thus, the given program was successfully executed and the output was verified as per the expected result.
VIVA QUESTIONS
What is string manipulation in Python?
String manipulation in Python refers to the process of modifying strings, such as changing their case, splitting them, joining them, or extracting substrings.
How do you find the longest word in a sentence using Python?
You can find the longest word in a sentence by splitting the sentence into words and using the max function with the key parameter set to len.
What does the slicing operation word[1:] + word[0] do in Python?
The slicing operation word[1:] + word[0] takes a word and shifts its first character to the end, effectively rotating the word to the left by one position.
Text File Line by Line
Read and process text files line by line
Remove Lines with Character
Remove specific lines from text files
Count Occurrences
Count character/word occurrences in files
Text File Remove Duplicate
Remove duplicate lines from text files
Binary File Search
Search records in binary files
Stack Implementation
Implement stack using lists
File Processing Strategy
Always read files line by line for memory efficiency when processing large files.
String Manipulation
Use built-in string methods like split(), strip(), and replace() for efficient text processing.
Error Handling
Always use try-except blocks when working with file operations to handle potential errors.