This article is the sixth part of this article series- ‘Python Chapters‘. In this am going to talk about Python Strings, its related operations, functioning in detail by using several examples and graphics.
So let’s explore it.
For getting the theme of Python, kindly go through my previous articles:
Chapter 1 | Python Properties
Chapter 2 | Python VS Other Languages
Chapter 3 | Environment and Setups
Chapter 4 | Python Basics 1.1
Chapter 5 | Python Basics 1.2
Chapter 6 | Python Strings…
Chapter 7 | Python Lists
Chapter 8 | Python Operators
Chapter 9 | Python Decision Making
Chapter 10 | Python Looping
Chapter 11 | Python Loop Control Statements
Chapter 12 | Python Tuples
Chapter 13 | Python Tuple Operations
Chapter 14 | Python Built-in Tuple Functions
Chapter 15 | Python Dictionary
Chapter 16 | Python Dictionary Operations
Chapter 17 | Python Function
Chapter 18 | Python Function Calling Arguments
Chapter 19 | Python Anonymous Function
Chapter 20 | Python Built-in Anonymous Function
So let’s explore the world of Python Strings..
Now am going to talk about string manipulation in Python. So, as we already know about string manipulation (being programmer); am not going to discuss it in detail. Just showing 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 string using single quotes as-
‘Goodbye World!’ # using Single quotes
Print (‘Goodbye World!’)
Strings | Double Quotes
Here’s alternate way of achieving same functionality, declaring it in double quotes:
‘Oops! I think I made a mistake. I meant “Hello World!”.’ # using Double quote
Print (‘Oops! I think I made a mistake. I meant “Hello World!”.’)
‘I meant- “Hello World!”. ’
Print (‘I meant- “Hello World!”. ’)
In general programming terms-
“Adding two strings is defined as String Concatenation.”
It can be done in the same way, as we do in some other programming language using a ‘+’ operator.
‘Being’ + ‘Programmer!’
Now, let’s take a look at second approach. Two or more string literals can also be concatenated automatically by writing them next to each other.
As, it’s name says it all. So am only going to explain how to do that rather than explaining what it is. So let’s take a look.
‘M’ + 2 * ‘iss’ + ‘ppi’
(I love this word, maybe because of Permutation & Combination and Probability Theory..)
I hope got you guys got the theme. Let me explain this 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 ‘*’ operator.
Let’s take a look at one more example for better understanding:
‘Ba’ + 2 * ‘na’
(I hope you guys can easily digest this example and concept as well.)
- Use ‘+’ 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 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 take a look at examples:
Fruit = ‘Ba’ + 2 * ‘na’
(Counting from LEFT)
Fruit = ‘Ba’ + 2 * ‘na’
(Counting from RIGHT)
Or you can think of it as-
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
Guidelines from our 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.
“Keep calm and code python”.
I tried to make it interesting and interactive article and wish you guys going to like that, meanwhile if you have any suggestions then your welcome.
Till the next part, Keep Sharing & Tweaking! 🙂