Curriculum
In this tutorial, you will learn about Strings in Python and how to use it in your python program with some examples.
String is a set/sequence of characters in python. They are enclosed by single or double quotation marks. We can say that strings are a set or an array of bytes of Unicode characters.
Example:
a= (‘Alpha, Beta, Gamma, Delta’) b=(‘5,6,7,8,9,0,’)
You may find this amusing; the value assigned to b has numbers as string. Yes, in python any value or set of characters enclosed by quotes either single or double is treated as string. No matter if it’s set of alphabets, numbers or special characters or the combination of all three.
In python, the elements of a string can be accessed by using Square brackets. The Subsets of strings can be accessed using ([ ] and [:]) with indexing from 0 to -1 at the end.
Input
a = ('Developer publish') print (a) print (a[0]) print (a[2:5]) print (a[2:])
Output
Developer publish D vel veloper publish
Look at the below string:
a= (“Developer Publish academy”)
Here we have used double quotes to enclose a set of characters in a line. We all know that single and double quotes are used only to enclose a string a single physical line. What if we need to use multiple lines of characters? We can’t use both single or double quotes.
That’s where the usage of tripe quotes comes in. You can use the triple quotes to represent or enclose a multiline string.
Take a look at the below example:
mulstr = (‘“Python programming course in Developer publish academy is the best in the market. All the lessons are neatly drafted and are very much understandable. You can learn programming easily by self-learning the courses”’) print (mulstr)
When you run the above code in python shell, you will get the following output.
Escape Sequence is a combination of characters with backslash that are non-printable and non-literal character interpretation. Backslash ( ) is used as the escape sequence initiator. The character prefixed by backslash is interpreted as an escape sequence. Each character has a different escape sequence. The following table lists all the escape sequence characters.
ESCAPE SEQUENCE | MEANING |
’ | Single quote |
” | Double quote |
backslash | |
n | New line |
r | Carriage Return |
t | Horizontal tab |
b | Backspace |
f | form feed |
v | vertical tab |
� | Null character |
N{name} | Unicode Character Database named Lookup |
uxxxxxxxx | Unicode Character with 16-bit hex value |
Uxxxxxxxx | Unicode Character with 32-bit hex value |
ooo | Character with octal value |
xhh | Character with hex value |
Sample program using escape characters/sequence in python
Input:
print("I study 'python_programming' at developer_publish_academy") print("The course are easy to learn and you can n earn while learning") print("The course are easy to earn and you can r earn while learning") print(“Now am learning aboutt escape characters in python”) print(“You can useeb to delete one e”)
Below are some of the common string operations that can be performed in Python. We’ll cover the entire list of Python string operations in the end of this course.
You can add two or more string together and make a new string using the concatenation operator (+) or you can print the string multiple times using (*) operator.
a=("Developer") b=("Publish") print(a+b+ "Academy") c=(a+b+ "Academy") print(c*2)
A for loop can be used to iterate through a string. Here’s a sample code snippet demonstrating how to iterate a string in python.
tobefound = 0 for letter in 'Developer Publish Academy': if(letter == 'e'): tobefound += 1 print(f'The letter e was found {tobefound} times' )
Using the membership operator we can check whether a character belongs to a particular string.
Here’s a sample code snippet in python showing how to check whether a character belongs to a particular string:
a=("Developer Publish Academy") print('e' in (a))
Here’s how you can change or update an existing string in python.
a= ("Developer Publish Academy") print ("Original string=",a) print ("String update=", a[0:8]+"d")
Learn more about python programming and programming languages at DeveloperPublish.com.
Try your programs at CodersEditor