String in Python
In this tutorial, we would learn about String in Python and Different String methods in Python. A string is a collection of characters. A String can contain alphabets, numbers or special characters. For example, the name of a person or address of a location can be store in a String.
How to create a String in Python:
To Create a String in Python we can write a sequence of character in Single quote, in double-quotes or even in triple quotes. Triple quotes are used to represent the multi-line String.
# Using '' quotes first_string = 'Owlbuddy' print(first_string) # Using "" quotes first_string = "Owlbuddy" print(first_string) # Using triple quotes first_string = '''owlbuddy''' print(first_string) # using triple quotes first_string = """Welcome to Owlbuddy.com""" print(first_string)
Accessing Characters of String in Python:
It is so easy to access Characters of string in Python. we can access characters of a string with the help of index number. For example, we have string “owlbuddy” if we want to access character “w” we know this character on index 1. So like this we can access string characters using an index number. we can also get small parts of a string using slicing operator (:). Check this example.
#Creating a string first_string = 'Owlbuddy' print('first_string= ', first_string) #first character print('first_string = ', first_string) #last character print('first_string[-1] = ', first_string[-1]) #slicing from 3rd to 5th character print('first_string[2:5] = ', first_string[2:5]) #slicing from 2nd to 2nd last character print('first_string[1:-2] = ', first_string[1:-2])
Concatenation of Strings in Python:
We can concatenate two different strings using + Operator and we can repeat a string multiple time by using * operator. Check this example.
first_string = 'Hello' second_string ='World!' # using + print(first_string + second_string) # using * print(first_string * 3)
Changing and deleting to a String:
Strings in Python are immutable. We can not change the string after once its initialized. We cannot change String but can delete entire string at once using the del keyword. Check this example.
first_string = 'Owlbuddy' # TypeError: 'first_string' object does not support item assignment first_string = 'o' # deleting string using del keyword del first_string # NameError: name 'first_string' is not defined print(first_string)
Some Common String Methods:
Here we will learn some common String methods in Python. Here is a list of methods.
first_string = 'Owlbuddy' # output: owlbuddy print(first_string.lower()) # output: OWLBUDDY print(first_string.upper()) # output: 'Hello How are you' ''.join(['Hello', 'How', 'are', 'you']) # output: ['Hello', 'How', 'are', 'you'] "Hello How are you".split(); # output: 5 print(first_string.find('d')) # output: Hello Owlbuddy (return copy of string after replacing) 'Hello World'.replace('World','Owlbuddy')
Escape Sequence in Python starts with a back slash and Python interpreter threat these Escape Sequences different the rest of the string. For example, you want to add a single quote in your string for words like (can’t or don’t) you can not do this by simply adding a single quote you have to add it like (\’) this in the string. It might be confusing right now. Check this example understand Escape Sequence in Python.
# using triple quotes print('''Hello, "What's up?"''') # escaping single quotes print('Hello, "What\'s up?"') # escaping double quotes print("Hello, \"What's up?\"")
List of Escape Sequence in Python:
|\newline||Backslash and newline ignored|
|\\||To add Backslash|
|\’||To add Single Quote|
|\”||To add double Quote|
|\”||To add double Quote|
Spread the love:
Please share this page on social media with your friends..