In this tutorial, we will discuss how to create a string in python. In my last blog, we discussed python variables.
We will also cover the below things as part of this tutorial
- Declare a string in python
- How to declare and assign a variable to a string
- Multiline strings in python
- Python String Functions
- How to check if a python string contains another string
Strings are sequence of character. A character can be a number,letter, special character etc.
A string can contain spaces and there is no limit on how many character it should contain.
Contents
Create a string in Python
Now, we will see how to create a string in python or how to declare a string in python?
We can declare a string in python in three ways
1- Using single quotes(‘ ‘) .
You can declare a string using single quotes(‘ ‘). Let,s see an example
Example:
print('Hello PythonGuides!')
Output will be
Hello PythonGuides!
2- Using double quotes(” “) .
You can declare a string using double quotes(” “). Let,s see an example
print("Hello PythonGuides!!")
Output will be
Hello PythonGuides!!
3- Using triple quotes(‘” “‘) .
You can declare a string using triple quotes(‘” ‘”). Let,s see an example
print("Hello PythonGuides!!!")
Output will be
Hello PythonGuides!!!
Now let’s run all these three formats and see the output.
You can put the quotes in side the string by keeping a backslash(\) directly before the quotes.
For example:
print ("Hello, \"I love python!\"")
Output will be
Hello, "I love python!"
How to declare and assign a variable to a string
Put the sequence of characters inside a single quote (‘ ‘) or double quotes(” “) or triple quotes(“”” “””) or (”’ ”’) to create the string and then assign it to a variable.
To assign it to a variable, we can use the variable name and “=” operator.
For Example:
x = "Python Guides!"
y = 'Python Guides!!'
z = """ Python Guides!!! """
print(x)
print(y)
print(z)
Normally single and double quotes are use to assign a string with single line of character but triple quotes are used to assign a string with multi lines of character.
Multiline strings in python
We can use triple quotes (“”” “””) or (”’ ”’) to assign a multiline string to a variable.
For Example:
x = """ Python Guides,
I love Python,
Python is the best!!. """
y = ''' Python Guides,
I love Python,
Python is the best!. '''
print(x)
print(y)
Output
Python Guides,
I love Python,
Python is the best!!.
Python Guides,
I love Python,
Python is the best!.
See the output here
How to get the length of a string
We can use the len() function to check length of a string in python.
Example:
x = "PythonGuides"
print(len(x))
y = "I love Python"
print(len(y))
z = "Python is the best"
print(len(z))
Output will be
12
13
18
Python String Functions
Python provides a set of build in methods to do different operation in string.
1- upper()
You can use upper() method to return the string in upper case.
Example:
x = "pythonguides"
print(x.upper())
Output will be
PYTHONGUIDES
2- lower()
You can use lower() method to return the string in lower case.
Example:
x = "PYTHONGUIDES"
print(x.lower())
Output will be
pythonguides
3- strip()
We can use strip() method to remove the white space if it presents on the starting of the string.
Example:
x = " PythonGuides"
print(x.strip())
Now see the output here
4- replace()
You can use replace() method to replace a character inside a string with another character or to replace a string with another string.
Example:
x = "I love Python"
print(x.replace("love","like")
Output will be
I like Python
5- split()
We can use split() method to split the string into substrings.
Example:
x = "python, guides"
print(x.split(","))
Output will be
['python', ' guides']
6- count()
We can use the count() method to count the number of character used in side a string.
Example:
x = "I love Python"
print(x.count('o'))
So we are asking how many time “o” is present inside the above string. The output will be 2.
7- center()
We can use the center() method to use to get the centered string.
Example
val = "PythonGuides"
y = val.center(20,"O")
print(y)
Output will be
OOOOPythonGuidesOOOO
Below are few other methods which are there to use with string
capitalize() : We can use this method to make the first letter of a string to upper case.
encode(): We can get an encoded version of a string using encode().
format(): We can format a value in a string.
title(): We can convert the first letter of the word as string using title() etc.
How to check if a python string contains another string
In other object oriented language, we can use contains method to check if a python string contains another string.
In python there are some ways to achieve this.
1- Using in operator
We can use in operator to check if a python string contains another string or not.
Using in operator is a clear and nit approach to check if the string contains another string.
Let’s see an example for this
Example:
mystring = "I love Python"
print("Python" in mystring)
print("Java" in mystring)
See the output here
In the above example we saw that since “Python” exists in the string so it returned True and since “Java” is not present inside the string so it returned False.
2- Using find() method
One more way to check if a python string contains another string or not is to use find(). Which is a python inbuilt method.
The find() method returns the first occurrence of the specified value.
It returns -1 if the value is not found.
Syntax
string.find(value, start, end)
Where start and end arguments are optional.
Let’s see an example for this
Example
mystring = "I love Python"
x = mystring.find("Python")
print(x)
Now see the output here
Example
x = mystring.find("Python")
y = mystring.find("java")
print(x)
print(y)
The out put will be 7 and – 1. – 1, since java is not present in side the string.
Example:
mystring = "I love Python very much"
x = mystring.find("o",2,8)
print(x)
See the output here
3- Using index() method
We can use the index() method to find the starting index of the first occurrence of a string inside the string.
If the string specified is not present inside the string then it will throw an exception.
Let’s see an example for the same
Syntax
string.index(value, start, end)
Where start and end are optional.
Example:
mystring = "I love Python very much"
x = mystring.index("Python")
print(x)
See the out put here
Example:
mystring = "I love Python very much"
x = mystring.index("o",2,8)
print(x)
You may like following below Python Tutorials:
- What is Python and What is the main use of Python?
- Why python is so popular? Why python is booming?
- How does python work? how python is interpreted?
- Python download and Installation steps
- Python Hello World Program
- How to create a variable in python
- Python String Functions
Python is the most popular open-source object-oriented programming language and it is easy to learn and syntax wise it is very simple.
Put the sequence of characters inside a single quote (‘ ‘) or double quotes(” “) or triple quotes(“”” “””) or (”’ ”’) to create the string and then assign it to a variable.
Python provides a set of build in methods to do different operation in string like upper(),lower(),count(),len(),format(),replace() etc
We can check if a python string contains another string using in operator,find() and index().
This python tutorial explains the below points:
- How to create a string in python
- Declare a string in python
- How to declare and assign a variable to a string
- Multiline strings in python
- Python String Functions
- How to check if a python string contains another string
Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.