Output of Python Program | Set 1 (Strings)
0 205
Output of Python Programs | Set 1 (Strings)
Strings are one of the most commonly used data types in Python. They represent sequences of characters and come with a variety of built-in methods to manipulate text. Understanding string operations is essential for any Python programmer.
Basic String Operations
Python strings can be concatenated, repeated, sliced, and indexed just like lists. Here’s a simple example demonstrating some common operations:
text = "Hello"
print(text + " World") # Concatenation
print(text * 3) # Repetition
print(text[1:4]) # Slicing
print(text[-1]) # Indexing from the end
Output:
Hello World
HelloHelloHello
ell
o
String Methods
Python strings have many helpful methods such as upper()
, lower()
, strip()
, and replace()
. These methods enable you to modify or inspect strings easily.
name = " Python Programming "
print(name.strip()) # Removes leading and trailing spaces
print(name.upper()) # Converts to uppercase
print(name.lower()) # Converts to lowercase
print(name.replace("Python", "Java")) # Replaces substring
Output:
Python Programming
PYTHON PROGRAMMING
python programming
Java Programming
Practice Problem
Consider the following Python program. Predict the output:
message = "GeeksForGeeks"
print(message[5:9])
print(message[::-1])
print(message.lower().count('e'))
Solution to Practice Problem
Output Explanation:
For the code above, the output will be:
ForG
skeeGrofskeeG
5
Explanation:
message[5:9]
extracts characters from index 5 to 8 which are 'ForG'.message[::-1]
reverses the string.message.lower().count('e')
converts the string to lowercase and counts the occurrences of 'e'.
Conclusion
Strings are extremely versatile in Python. Familiarity with basic operations and built-in methods can make your programming tasks easier, especially when dealing with text processing. Try experimenting with different string methods to get a better grasp of their behavior.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments