String in Python

In this tutorial we would learn about String in Python and Different String methods in Python. String is a collection of characters. A String can contain alphabets, numbers or special characters. For example name of person or address of a location can be store in a String.

String in Python, String methods in Python

How to create a String in Python:

To Create a String in Python we can write sequence of character in Single quote, in double quotes or even in triple quotes. Triple quotes are use to represent multi-line String. Here is an example to show how we can create string in Python.


# Using '' quotes 
first_string = 'Owlbuddy'

# Using "" quotes 
first_string = "Owlbuddy"

# Using triple quotes 
first_string = '''owlbuddy'''

# using triple quotes
first_string = """Welcome to 

Accessing Characters of String in Python:

It is so easy to access Characters of string in Python. we can access characters of 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 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[0] = ', first_string[0])

#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 string after once its initialized. We cannot change String but can delete entire string at once using del keyword. Check this example.


first_string = 'Owlbuddy'

# TypeError: 'first_string' object does not support item assignment
first_string[0] = 'o'

#  deleting string using del keyword
del first_string 

# NameError: name 'first_string' is not defined

Some Common String Methods in Python:

Here we will learn some common String methods in Python. Here is list of methods.
  • lower()
  • upper()
  • join()
  • split()
  • find()
  • replace()


first_string = 'Owlbuddy'

# output: owlbuddy

# output: OWLBUDDY

# output: 'Hello How are you'
''.join(['Hello', 'How', 'are', 'you'])

# output: ['Hello', 'How', 'are', 'you']
"Hello How are you".split();

# output: 5

# output: Hello Owlbuddy (return copy of string after replacing)
'Hello World'.replace('World','Owlbuddy')

Escape Sequence:

Escape Sequence in Python starts with 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 you 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 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:

Escape Sequence Description
\newline Backslash and newline ignored
\\ To add Backslash
\' To add Single Quote
\" To add double Quote
\" To add double Quote

String in PythonClick Here to Download Jupyter Notebook file of This Tutorial.

Share this tutorial with your friends on your social Media..

String in Python String in Python String in Python String in Python

Now You Can Follow All These Tutorials on Your Android Mobile Just Download This App.