Strings in Python Part-1

Dhwani mehta
3 min readJan 10, 2023

--

What is string?

  • It’s an ordered Sequence. And Python keeps track of every element in the string as sequence.
  • for Example :
  • We have written ‘HELLO’ then Python understands the string to be in specific order. Which means we will be able to use indexing to grab particular letters.

Note : I am using Jupyter notebook and because of which without using print() function getting the output. But correct way to display string is using print() function.

How to create a string?

we can create a string in Python using single quotes and Double Quotes

  • for Example:
1.1

Be aware with quotes!

1.2
  • In the above 1.2 reason for the error is single quote in I’m stopped the string.

We can use combination of single quotes and double quotes to get desired output. as shown in 1.3

1.3

Multiline Strings

can assign a multiline string to a variable by using three quotes

can use three double quotes:
Or three single quotes:

How to print a string?

  • We can use print () function
  • for Example:
1.4

Use of len() function to check the length of the string!

Python’s built-in len() function counts all of the characters in the string, including spaces and punctuation.

len function

How to do Indexing in String?

we know strings are in ordered sequence. and we can use those indexes to call parts of it.

Indexing

here, [ ] square bracket is used after an object to call it’s Index. Important note is Indexing start from 0 in python.

we can also access a string by negative indexing. which starts from -1

Now How to grab all the element together or to grab some specific part of it?

so for that we can use slicing to achieve this task.

string slicing

mystring[-1] means the last letter (one index behind 0 so it loops back around)

Grab everything but the last letter

step size in more detail:

Grab everything, but go in steps size of 1
Grab everything, but go in step sizes of 2

String Properties:

It has most important property of Immutability. Which means once the string is created, we can not change the elements within it.

for example:

Reassignment is not allowed

We can’t slice the string and get the information but we can concate the string

concatenate strings

We can use the multiplication symbol to create repetition!

create repetition

--

--

Dhwani mehta

Passion with Pyhton and Experience Hadoop developer, with a demonstrated history of working in the IT industry.