Python Strings Tutorials with Examples, Welcome to the world of Strings in Python Tutorial. Are you the one who is looking forward to knowing the Introduction to Strings in Python? Or the one who is very keen to explore the Python Strings Tutorials with Examples that are available? Then you’ve landed on the Right path which provides the standard information of Strings in Python.
Here, we will learn about what is Python strings, Python string literals, How to assign strings to a variable, Multiline assignment strings, and Concatenation of strings and more. If you are the one who wanted to become an expert in Python? Or the one who wants to explore the technology like a Pro under the certified experts with world-class training environment, then asks your Python training institute experts who offer Advanced advanced advanced Python training. Follow the below-mentioned Python strings tutorial with examples and enhance your skills to become a professional Python Developer.
What is Python Strings?
A Python string is a sequence of collection of characters to represent and store the text-based information. Strings can be accessed by both directions - forward and backward and stored as individual characters in a contiguous memory location. Strings cannot be changed once they have created as they are immutable data types in python.
String Literals in Python
In Python, strings can be written with a single or double quotation marks, even with triple quotation marks also.
Example:
print(‘Python’)
print(“Python”)
print('''python''')
All of the above will give the same result by printing Python
How to Assign String to a Variable
A variable name is allotted to string as follows:
P=’python’
print(P)
Multiline Strings in Python
We can print multiple lines or statements using a variable with the help of single or double quotes 3 times as follows:
Test="""Welcome to...
...python world"""
print(Test)
It will give output as same as lines written in quotes marks above.
Similarly, we can use a single quotation mark as:
test='''Welcome to python world'''
print(test)
Concatenation of Python Strings
We can join two different strings by concatenation as follows:
X= ‘wireless’
Y=’network’
print( X+Y)
Output: wireless network
If we want to put a space between the strings we can print it as:
print (X+" "+Y)
Output:
wireless network
Strings as arrays
Python doesn’t have a character data type, a single character is a string with a length of 1.We can access strings as an array. Like arrays, we can access letters (elements) from string with index position. The starting position is 0 means index starts with 0.
Tup=’ python’
print(Tup[0])
Output:
P
Likewise, we can access all elements of a string with their index positions.
String Slicing
We can access the specific part of the string which is nothing but slicing strings. By specifying starting and ending position indices positions we can select a particular part of the string.
A=’python’
print(A[2:])
Output
thon
print(A[1:4])
Output:
yth
# In case of negative indexing, it will count the letters from the end of the strings and gives result accordingly
print(A[-4:-2])
Output:
th
String Functions in Python
Strip():
It removes any white spaces from the beginning of the end.
Ex) b = “Hello python”
print(b.strip())
Output:
Hello python (The space before Hello is removed)
Len():
It returns the length of the string.
Ex) Test = “Pythonworld”
print(len(Test))
Output:
11
lower():
It returns the string into a lower case.
Ex) test= “Hello PyThon”
print(test.lower())
Output:
Hellopython
upper():
It returns the string into the upper case.
Ex) test= “Hello python”
print(test.upper())
Otuput:
HELLOPYTHON
replace():
It replaces the string with another string.
Ex) test = “Tree”
print(test.replace(“T”, “F”)
Otuput:
Free
format():
It inserts the number into a string
Ex) score = 85
txt = "Your Maths Score is {}"
print(txt.format(score))
Output:
Your Maths Score is 85
count():
It will count the number of occurrences of the specified characters.
Ex) c='This tuple is immutable'
c.count('i')
Output:
3
find():
It will give the index position of the string by searching it.
Ex)c='This tuple is immutable'
c.find('is')
Output:
2
Split():
It Splits the string at the specified separator, and we get it as a list
Ex) test='Primary'
test.split('r')
Output:
['P', 'ima', 'y']
startswith():
It will return output in the form of True or False depending on whether the mentioned character is present in the given string.
Ex) Y= ‘network’
Y.startswith('w')
Output:
False
Z=’wireless’
Z.startswith(‘w’)
Output:
True
For checking whether the character or phrase is present in the given string we can use in or not in
Ex)test= "Tuples are immutable but lists are mutable"
f = 'are' in test
print(f)
Output:
True
Similarly, if we take a character or a word which is not available in the above statement then,
test= "Tuples are immutable but lists are mutable"
f = 'sets' in test
print(f)
Output:
False
We can achieve the exact opposite result for not in
test= "Tuples are immutable but lists are mutable"
f = 'are' not in test
print(f)
Output:
False
Similarly, if we take a character or a word which is not available in the above statement then,
test= "Tuples are immutable but lists are mutable"
f = 'sets' not in test
print(f)
Output:
True
We hope you understand sets in Python strings tutorial with examples. Get success in your career as a Python developer by being a part of the Prwatech, India's leading Python training institute in Bangalore.