Diving Into Python : Chapter 6

Hello guys.
 
This article is the sixth part of this article series. This will talk about string, its related operations, functioning in detail by using several examples and graphics. So let’s explore it.
 
Getting Theme
 
For getting the theme of Python, kindly go through my previous articles:
Strings Manipulation
 
Now I will talk about string manipulation in Python. So, since we already know about string manipulation (being programmers) I will not explain it in detail. I will just show how easily you can do that in Python.
 
 
Expressing a String
 
It can be expressed in several ways in Python.
 
Strings | Single Quotes
 
You can manipulate a string using single quotes.
 
For Example
 
‘Goodbye World!’ # using Single quotes
 
Snippet: print(‘Goodbye World!’)
 
Strings | Double Quotes
 
Here’s an alternate way of achieving the same functionality, declaring it in double-quotes.
 
For Example
 
‘Oops! I think I made a mistake. I meant “Hello World!”.’ # using Double quote
 
Snippet: print(‘Oops! I think I made a mistake. I meant “Hello World!”.’)
 
Or
 
‘I meant- “Hello World!”. ’
 
Snippet: print(‘I meant- “Hello World!”. ’)
 
String Concatenation
 
In general programming terms:
 
“Adding two strings is defined as String Concatenation.”
 
Approach 1
 
It can be done in the same way, as we do in some other programming language using a ‘+’ operator.
 
For Example
 
‘Being’ + ‘Programmer!’
 
‘BiengProgrammer!’
 
Approach 2
 
Now, let’s have a look at the second approach. Two or more string literals can also be concatenated automatically by writing them next to each other.
 
For Example
 
‘Being’ ‘Programmer!’
 
‘BiengProgrammer!’
 
String Repetition
 
Its name says it all. So I will only explain how to do that rather than explaining what it is. So let’s have a look.
 
For Example
 
‘M’ + 2 * ‘iss’ + ‘ppi’
 
‘Mississippi’
 
(I love this word, maybe because of Permutation & Combination and Probability Theory.)
 
I hope you guys now understand the theme. Let me explain this with an example.
 
As you guys can see, I used a ‘*’ operator just before the string ‘iss’ and you can see what we got. So string repetition works in this fashion using the ‘*’ operator.
 
Let’s have a look at one more example for a better understanding.
 
For Example:
 
‘Ba’ + 2 * ‘na’
 
‘Banana’
 
(I hope you guys can easily digest this example and the concept as well.)
 
PinPoints
  • Use the ‘+’ operator for adding two strings
  • Use ‘*’ operator for string repetition.
  • Concatenation works only with literals (Approach 2)
  • You can do that in case of expressions and variables.
String Indexing
 
Strings can be indexed, with the default indexes. In that first character always contain index 0 and last n (where n can be any natural number).
 
Let’s have a look at examples.
 
For Example
 
Fruit = ‘Ba’ + 2 * ‘na’
 
(Counting from LEFT)
 
Word[0]
 
‘B’
 
Word[1]
 
‘a’
 
Word[2]
 
‘n’
 
Word[3]
 
‘a’
 
Word[4]
 
‘n’
 
Word[5]
 
‘a’
 
Or
 
Fruit = ‘Ba’ + 2 * ‘na’
 
(Counting from RIGHT)
 
Word[-6]
 
‘B’
 
Word[-5]
 
‘a’
 
Word[-4]
 
‘n’
 
Word[-3]
 
‘a’
 
Word[-2]
 
‘n’
 
Word[-1]
 
‘a’
 
Or you can think of it as:
+ + + + + + Indexing
B a n a n a  
0 1 2 3 4 5 Left(Pos)
-6 -5 -4 -3 -2 -1 Right(Neg)
 
PinPoints
 
The following are points to remember:
 
  • Either you can count from ‘LEFT’
  • or you can do from ‘RIGHT’
  • As 0 and -0 are same so, Right indices start from -1
The following  are guidelines from my side:
  • Do as much as code you can.
  • Code anything you want, the best way to learn
  • Don’t just study things, try to learn them.
  • Work on your Concepts
  • Work on the fundamentals of any technology or stuff you want to learn.
Moto
“Keep calm and code Python”.
 
I tried to make this an interesting and interactive article and wish you guys it, meanwhile, if you have any suggestions then your welcome.
 
Until the next part, keep sharing.
 
Happy Learning.