Python Strings
Last Update : 09 Dec, 2022In this tutorial, you will learn about Python Strings.
A string is a sequence of characters.
Python Strings are surrounded by either double quotation marks or single quotation marks.
Both 'uxPython', and "uxPython" strings are the same.
You can use the print() function to display a String literal.
Example -:
print("uxPython")
This program produces the following result -:
uxPython
How to assign a String to a variable?
You can assign a string value to a variable by doing with the variable name followed by an equal character and the string.
Example -:
var = "uxPython"
Multiline Strings in Python
You can make a multiline string by using three double quotes or three single quotes.
# multiline string using three double quotes.
var1 = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt"""
# multiline string using three single quotes.
var2 = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt'''
Accessing Characters of Python Strings
Python strings are arrays of bytes representing Unicode characters. But, there is no character data type in Python. A single character is a string with a length of 1.
You can use square brackets to access elements of the String. Also, the first character has the position 0.
Example -:
var = 'uxPython'
print('var = ', var)
#first character
print('var[0] = ', var[0])
#last character
print('var[-1] = ', var[-1])
#slicing 2nd to 5th character
print('var[1:5] = ', var[1:5])
#slicing 6th to 2nd last character
print('var[5:-2] = ', var[5:-2])
This program produces the following result -:
var = uxPython
var[0] = u
var[-1] = n
var[1:5] = xPyt
var[5:-2] = h
Looping through the characters in a String
We can use Python for loop for looping through the letters in a string.
The following example shows looping through the letters in the word "uxPython".
txt = "uxpython"
for x in txt:
print(x)
This program produces the following result -:
u
x
p
y
t
h
o
n
How to get the String length
We can use the len() function to get the length of a string.
The following example returns the length of a string "uxpython".
txt = "uxpython
print(len(txt))
This program produces the following result -:
8
How to check specific phrases or character in a String.
we can use the keyword in to check if a specific phrase or character is present in a string.
The following example checks if the word "best" is present in the following text.
txt = "Python is the best programming language"
# check without the if statement
print("best" in txt)
# check with the if statement
if "best" in txt:
print("The word 'best' is present.")
This program produces the following result -:
True
The word 'best' is present.
How to check specific phrases or character not available in a String.
we can use the keyword not in to check if a specific phrase or character is present in a string.
The following example checks if the word "bad" is not present in the following text.
txt = "Python is the best programming language"
# check without the if statement
print("bad" not in txt)
# check with the if statement
if "bad" not in txt:
print("The word 'bad' is not present.")
This program produces the following result -:
True
The word 'bad' is not present.