String in Python

String is basically “sequence of characters”.

Characteristics of String:

  • Indexing in a string:

It is used to refer to a particular character present in string, considering the counting starts from 0.

Example:

parrot=”Blue” 

print(parrot[0])

//output: B

  • Negative Indexing in String:

It is used to count backward or from end of string

Example:

parrot=”Blue”

print(parrot[-1])

// output: e

i.e String Length – Positive Index Number= Negative Index Number

  • Slicing:

It is used to slice the string and print character from start position to end position or extract a portion of string and print it .

Example:

parrot=”Norwegian”

print(parrot[0:6])

//output:Norweg

i.e above output printed characters from 0 (start position to upto 6 but not including 6th character )

Note: If we leave start value it defaults to start position

Example:

print(parrot[ :6])

//output:Norweg

           If we leave end value it defaults to last character

Example:

print(parrot[2: ])

//output:rwegian

  • Step in a Slice:

Step is the stride between indices .

So the syntax is string[start:stop:step]

Example: Let’s say you have the string my_string = “Hello, Python!” and you want to extract every second character from the first five characters:

my_string = "Hello, Python!

print(my_string[0:5:2])

//output:Hlo

  • Slicing Backwards or Reverse Slicing:

With a negative step value , start value default to end of string and stop value default to start of string if not given.

Example:

letters="Blue"

print[3:0:-1]

//output:eul

print[3::-1]

//output:eulB

Common String Operations :

  • String Concatenation :

This operation is used to add two string .

Example:

string1=”Hello”        string2=”World”

print(string1+string2)

//output: “HelloWorld”

  • Repetition/ Multiplication:

This operation is used to repeat the given string “n” number of times.

Example:

print(“Hello” * 5)

//output :”HelloHelloHelloHelloHello”

  • Substring in a string:

“in” operator is used to find substring in string.

Example:

today=”Friday"

print(“day” in  today)

//output: True

above output returns a True (bool value ) if day is a substring in Friday else it returns False(bool value)

  • String Replacement Fields:

In Python every data type can be coerced into a string representation using “str”. Example:

age =24

print(“My age is “ + age )

//output :Error

As we are trying to concatenate two different data type string and integer . To do so we have to change type of age . So

print(“My age is “ + str(age) +” years“)

//output: My age is 24 years.

  • String Formatting :

Python 3 allows string to be formatted using replacement field and dot method.

Example:

age =24

print(“My age is  {0} years “.format(age))

//output: My age is 24 years

In the above output the value inside {} is replaced by the variable in format .

print(“ There are {0}  days in {1} {2}”.format(31, “jan”, “mar”))

0 here is for first value in format ,

1 for second value and so on.

Leave a Reply

Your email address will not be published. Required fields are marked *