Visualizing the phenomenal growth of Electric Vehicles

It is amazing to see how far we have come within a decade when it comes to Electric Vehicles. Although Tesla being the pioneer is synonymous with the EV revolution, the consumers now have a lot more…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Strings in Python

Strings are the sequence of characters . We can use single or double quote to represent a string:

If your string contains single quote as mentioned above, then wrap the string in double quotes, because

‘I’m playing cricket’ — will result in error

To escape from this error, we can use escape character also:

‘I\’m playing cricket’

We can use indexing and slicing to grab the sub-sections of a string. Indexing uses the [] notation after the string. You can access single character from a string.

e.g. let’s consider a string, “india”

so, index of i is 0, index of n is 1 and same index of a is 5. In short, indexing for “india” is like 0 1 2 3 4 .

Reverse indexing for string “INDIA” is -5 -4 -3 -2 -1

length of string is calculated using len() function. len() function treats white spaces as a character.

Slicing allows you to access multiple characters of a string. Syntax for slicing a string is :

[start:stop:step]

start: numerical index for slice start

stop: index you will go upto

step: size of “jump” you take

e.g. Slicing will start from index from 0 to 7 and jumping by 2 positions.

let’s see more examples:

Reverse of a string can be done as below:

Immutability of a string:

Strings are immutable. It means you cannot change the content of a string.

You can change the content of string by means of concatenation.

Let’s say I want to change P in ‘Python’ to A to make it ‘Aython’.

Let’s explore more:

String Formatting:

There are two methods for formatting:

.format() method had taken string ‘ankit’ and kept in the place of curly braces.

2. f-strings (formatted string literals) (Python 3.6 update)

Differences in printing in Python 2 and Python 3:

Python 2:

Python 3:

In Python 3, print is a function, not a statement.

Happy Coding!

Add a comment

Related posts:

I saw you today.

Thanks for reading! Did this piece make you scoff, nod in agreement, dry heave or react otherwise? Leave me a comment and tell me what you think! If you are intrigued and/or like my writings and my…

Uninterrupted time

A span of uninterrupted time is necessary to let certain types of creativity emerge. Similarly in meditation, if you let yourself stay in quietness for some time, uninterrupted, only then you might…

10 Tips Penting Untuk Menjadi Scrum Master Hebat

Tulisan ini terinspirasi dari “Ten sentences with all the Scrum Master advice you’ll ever need” oleh Mike Cohn. Selain 10 hal di atas, tentu masih banyak tips lain yang perlu kita perhatikan sebagai…