String Handling in Python: Complete Beginner Guide
String Handling in Python: Complete Beginner Guide
String are one of the most commonly used data types in python. Whether you're working with user input, files, or APIs, understanding string handling is essential.
What is a String in Python?
A string is a sequence of characters enclosed in quotes. Strings are immutable i.e., cannot be changed after creation. String can be depicted inside single quotes (' '), double quotes(" ") and Triple quotes (''' '''). Triple quotes are used for multiline string i.e. writing string in multiple lines, we can write string in multi-line with single and double string too but for that either you have to do following:
text="happy"
next="birthday"
OR
text="happy\n birthday"
both of the above code will give you same output as follows:
happy
birthday
But with triple quote we can simply write as :
#''' This is python blogging page,
where you learn more and
grow your programming knowlege '''
Accessing Characters in a String
Python allows indexing starting from 0 and its also allow negative indexing starting from -1.
text = "Python"
print(text[0]) #P
print(text[3]) #h
print(text[-1]) #n
String Slicing
Extract parts or substring from the string itself with the help of slicing.
text = "Python Programming"
print(text[0:6]) #Python
print(text[7:]) #Programming
print(text[:6]) #Python
String Operations
Concatenation
It is used for making two string as one string with the help of (+) concatenation. For example:
a="Hello"
b="World"
print(a+" "+b) #Hello World
Repetition
When we use multiplication sign it act as repetition i.e., that is it repeat the number of times it multiplied with.
print("Hi" * 3) #Hi Hi Hi
Common String Methods
Python provides many built-in functions for string handling.
Upper function
Converts each letter in the text into upper case. Example given as following:
text="Python programming"
print(text.upper()) #PYTHON PROGRAMMING
Lower function
Coverts each letter in the text into lower case. Example given as following
print(text.lower()) #python programming
Capitalize function
Converts first letter of sentence to capital letter. Example given as follows:
print(text.capitalize()) #Python programming
Title function
Convert first letter of each word into capital letter. Example given as follows:
print(text.title()) # Python Programming
Searching in Strings
Find function
Used to find word from the sentence .
text = "Python is powerful"
print(text.find("is")) # return index
print("Python" in text) #True
Replacing Text
Replace function
Replace function is used to replace one string from the other.
text="I love Java"
print(text.replace('java','Python')) # I love Python
Splitting and Joining
Split function
Split function is used for splitting the text from a certain symbol that you specify in the function.
text="apple,banana,orange"
fruits=text.split(",")
print(fruits) # ["apple", "banana", "orange"]
Join function
Join function is used for joining the text together with the help of connecting string you will mention in the code. For example let's take above text:
joined="-".join(fruits)
print(joined)
Removing Spaces
text=" Hello World "
print(text.strip()) # remove space from both the side of the string
print(text.lstrip()) # remove space from left side
print(text.rstrip())# remove space from right side
Output:
Hello World
Hello World
Hello World
String Formatting
Old Method
name = "Astha"
print("Hello %s" % name)#Hello Astha
Modern Method(f-strings)
name="Astha"
age=22
print(f"My name is {name} and I am {age} years old")#My name is Astha and I am 22 years old
Real-life Example
email="user@gmail.com"
if "@gmail.com" in email:
print("Valid Gmail account")
Practice Challenge
Try this:
- Count how many vowels are in a string.
- Reverse a string without using built-in functions
Comments
Post a Comment