Skip to main content

Write a python program for STRING manipulation method.

Description

The provided Python code operates on a text string, showcasing how to change its case to uppercase and lowercase, split it into words, and attempt to reverse it.

Code

program8a.py
text='Welcome to M.L.Bharatesh Polytechnic'
print(text)
print("to convert upper case:")
print(text.upper())
print("to convert lower case:")
print(text.lower())
word=(text.split())
print("after spliting:")
print(word)
print(list(reversed(text)))

Explanation of above code

  • String Initialization: The code initializes a variable 'text' as a text string with the value 'Welcome to M.L.Bharatesh Polytechnic.' This string serves as the primary data for various text manipulation operations.
  • Printing the Original Text: After initializing the 'text' string, the code utilizes the 'print' function, a core Python feature, to visually display the original content of the 'text' string. This step establishes a clear starting point, enabling programmers to understand and verify the initial data.
  • Converting to Uppercase: The code employs Python's 'upper()' method, which is specific to strings. This method is used to transform the 'text' string into uppercase characters. Every character within the 'text' string, irrespective of its original case, becomes capitalized. As a result, 'Welcome to M.L.Bharatesh Polytechnic' is converted to 'WELCOME TO M.L.BHARATESH POLYTECHNIC.'
  • Printing the Uppercase Text: Following the uppercase conversion, the code proceeds to print the modified 'text' string. The printed output showcases the 'text' in its uppercase form, offering a visual confirmation of the transformation.
  • Converting to Lowercase: The code moves forward with another transformation by utilizing the 'lower()' method, a counterpart to 'upper()' in Python's string manipulation arsenal. The 'text' string is subjected to this method, leading to the conversion of all characters to lowercase. Each character within the 'text' string transitions to its lowercase equivalent, resulting in the text 'welcome to m.l.bharatesh polytechnic.'
  • Printing the Lowercase Text: Subsequent to the successful conversion to lowercase, the code proceeds to print the modified 'text.' The printed output showcases the 'text' in its lowercase state, confirming the effect of the transformation and providing a clear visual representation.
  • Splitting the Text: The code advances to another operation, employing the 'split()' method, a fundamental tool for string manipulation in Python. 'split()' is applied to the 'text' string, segmenting it into individual words. By default, the method separates the string into words based on spaces, effectively creating a list of words. Each element in the list represents a distinct word from the original text.
  • Printing the List of Words: Immediately following the text splitting process, the code prints the outcome – the list of words stored within the variable 'word.' The printed display exhibits each word as an individual element within the list. This visual representation of segmented words aids in comprehending the transformation and confirms the successful operation.
  • Reversing the Text: The code embarks on a different aspect of string manipulation, attempting to reverse the order of characters within the 'text' string. To accomplish this, it utilizes the 'reversed()' function, a feature of Python that facilitates the reversal of iterable objects. However, it's crucial to note that the result of 'reversed(text)' is an iterator, not a direct list of characters. Consequently, the 'reversed(text)' outcome represents an iterator, necessitating further processing to achieve the intended reversed text output.
  • Printing the Reversed Text: The code proceeds to print the result of 'reversed(text)' in an attempt to display the reversed text. Nonetheless, due to the nature of the 'reversed()' function, which yields an iterator, the output may not align with expectations. It's imperative to understand that, for the successful display of reversed text, the iterator should be transformed into a list, followed by a subsequent reformation into a string. This critical consideration ensures the effective reversal of the text string and its accurate representation.

Learn more

Reference