In this Article we will tell you What is String in Python and will explain the topic in brief with examples. We will also learn-
- What are Empty Strings
- What are Multi-line Strings
- Iterating String
- Concatenating Strings
- Traversing String
- Question on Strings in Python
- and some other topics....
What are Strings in Python?
This Sequence of character may include any type of data which can be integers, floats, special character or a backslash, but they all are considered as the part of a String.
For example -:
A = "Adding 2 and 2.5 we get 4.5"
print ( A )
print ( A )
In this we have used alphabets ,integers and floats but they all are placed within ‘ ‘ single or “ “ double Quotes which means they are not an integer or float they are string.
Here A is a variable with the values -: Adding 2 and 2.5 we get 4.5.
[ = ] Operator is used to store the values in the variable.
When we print this code then we get the output as,
Adding 2 and 2.5 we get 4.5
If we want to check the type of the variable then we have to simply write,
type ( A )
<class ' str ' >
<class ' str ' >
Creating Strings
Strings are the most popular data type in Python. We can create them simply by using the single, double and triple quotes as we have seen above.
Single Quotes are treated the same as the double quotes and triple quotes are mainly used for the multi line strings or doc strings.
Empty String
An Empty String is the string which is having 0 characters. These are created to add the data inside them
.
To create an Empty String we use Quotes with no value or zero character.
For example-:
A = ""
print ( A )
Output-:
print ( A )
Output-:
Multi-Line String
Multi line Strings are those which are represented using triple Quotes.
x = '''this is a
multi-line string
example '''
print(x)
Output-:
multi-line string
example '''
print(x)
Output-:
this is a
multi-line string
example
What if we want to add double or single quotes inside a String?
For example -: " This article represents " Strings " briefly "
To insert the quotes inside a String we simply use backslash which indicates special nature of the characters.
A = " This \ " Article \ " is based on String."
print ( A )
Output-: This "Article" is based on String .
But What if we do not use backslash ( \ )?
If we do not use backslash then we will get an error of Syntax. (SYNTAX ERROR)
Traversing String
Traversing means accessing all the elements of the string using the index or subscript value.
Each elements in a string are present in a blocks.
For example -:
str = "Computer"
print str1[ 0 ] Output-: C
print str1[ 1 ] Output-: o
print str1[ 2 ] Output-: m
print str1[ 3 ] Output-: p
print str1[ 4 ] Output-: u
print str1[ 5 ] Output-: t
print str1[ 6 ] Output-: e
print str1[ 7 ] Output-: r
print str1[ 0 ] Output-: C
print str1[ 1 ] Output-: o
print str1[ 2 ] Output-: m
print str1[ 3 ] Output-: p
print str1[ 4 ] Output-: u
print str1[ 5 ] Output-: t
print str1[ 6 ] Output-: e
print str1[ 7 ] Output-: r
Here 0,1,2,3,4,5,6,7 are the index value of the variable str.
We can Start the Index values from Left or Right.
Index values are of two types -:
- Positive Index value
- Negative Index value
Positive Index value
Positive index value are those which start from left (0) and moves toward right.
Negative Index values
Negative Index values are those which starts from right (-1) and move towards left.
Iterating Strings using Loops
Each character of the String can be accessed using loops.
str = "Computer"
for i in str :
print(i)
print(i)
Output-:
C
o
m
p
u
t
e
r
Special String Operators
Special strings can be manipulated using operator like- Concatenation
- Replication , and
- Membership
Concatenating String
Concatenating String refers to creating a new String using two String. This can be done using the operator ( + ) between the two Strings .
y = "Hello" + "World"
print( y )
print( y )
Output-: HelloWorld
# To give a space between the two string insert a space before the closing Quote of the first String
x = "Hello " + "World"
print( x )
Output -: Hello World
These are only used to add the two string but if you add a string with an integer then it will give an error.
z = 5 + "hello"
print ( z )
Output-:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
z= 2 +"hello"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
print ( z )
Output-:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
z= 2 +"hello"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Replicating Strings
Replicating Strings is used to create multiple copies of the String. This is done using this operator (*). For example if we want to make 5 copies of Hello then we use it as-:
A = "Hello" * 5
print( A )
Output-: Hello Hello Hello Hello Hello
B = '2' * 3
print( B )
Output-: 2 2 2
print( A )
Output-: Hello Hello Hello Hello Hello
B = '2' * 3
print( B )
Output-: 2 2 2
Membership Operator
Python Strings has two Membership Operator which are- in Operator
- not in Operator
In ( in ) Operator in Python Strings
It returns True if the character/Sub-string does not exist in the given String.Not In ( not in ) Operator in Python String
It return True if character/String does not exist in the given string.
x = "Hello"
>>> 'H' in x
True
>>> 'h' in x
False
>>> 'H' in x
True
>>> 'h' in x
False
Comparison Operator
Comparison Operator are used to compare the two strings. Comparison Strings include the Following -:- ==
- >
- >=
- <=
- <
- !=
>>> "hello"=="hello"
True
>>> "hello"=="Hello"
False
>>> "wall" > "wall"
False
>>> "Wall" > "wall"
False
>>> "wall" > "Wall"
True
>>> "hello" != "hello"
False
True
>>> "hello"=="Hello"
False
>>> "wall" > "wall"
False
>>> "Wall" > "wall"
False
>>> "wall" > "Wall"
True
>>> "hello" != "hello"
False
0 Comments