Search This Blog

Wednesday, 15 December 2021

Strings in Python

 

Python Strings

html5 templates

Python string is a built-in type text sequence. It is used to handle textual data in python. Python Strings are immutable sequences of Unicode points. Creating Strings are simplest and easy to use in Python.

We can simply create Python String by enclosing a text in single as well as double quotes. Python treat both single and double quotes statements same.

Example : 

a = "Hello"
b= 'Java'
print(a+" "+b)

output of above example  :

Hello Java

Accessing Python Strings

  • In Python, Strings are stored as individual characters in a contiguous memory location
  • The benefit of using String is that it can be accessed from both the directions (forward and backward).
  • Both forward as well as backward indexing are provided using Strings in Python.
  • Forward indexing starts with 0,1,2,3,.... 
  • Backward indexing starts with -1,-2,-3,-4,....
str[0]='P'=str[-6] ,
str[1]='Y' = str[-5] ,
str[2] = 'T' = str[-4] ,
str[3] = 'H' = str[-3] ,
str[4] = 'O' = str[-2] ,
str[5] = 'N' = str[-1].

Python String Example

Here, we are creating a simple program to retrieve String in reverse as well as normal form.

name="NOTES"
length=len(name)
i=0

for n in range(-1,(-length-1),-1):
    print(name[i],"\t",name[n])
    i+=1

output : 

N         S

O         E

T         T

E         O

S         N

Python Strings Operators

To perform operation on string, Python provides basically 3 types of Operators that are given below.

  • Basic Operators.
  • Membership Operators.
  • Relational Operators.

1. Basic Operators

There are two types of basic operators in String + and *.

String Concatenation Operator (+)

str1="Java"
str2="Notes"
print(str1+str2)

Output :

JavaNotes

String Concatenation Operator (+)

ExpressionOutput
"20"+"40""2040"
"java"+"123""java123"
"java01"+"notes01""java01notes01"

NOTE: Both the operands passed for concatenation must be of same type, else it will show an error.

for example :
print("java"+01)

output :

Traceback (most recent call last):
  File "main.py", line 1, in
    print("java"+01)
    TypeError: Can't convert 'int' object to str implicitly

Python String Replication Operator (*)

Replication operator uses two parameters for operation, One is the integer value and the other one is the String argument.


The Replication operator is used to repeat a string number of times. The string will be repeated the number of times which is given by the integer value.


For Example :

print("java" * 5)
print(3 * "Python")

output : 

javajavajavajavajava
PythonPythonPython

Python String Replication Operator (*)

ExpressionOutput
"ArcX" * 2"ArcXArcX"
3 * '5''555'
'@' * 5'@@@@@'

NOTE: We can use Replication operator in any way i.e., int * string or string * int. Both the parameters passed cannot be of same type.

2 . Python String Membership Operators

Membership Operators are already discussed in the Operators section. Let see with context of String.


There are two types of Membership operators :

  1. in - "in" operator returns true if a character or the entire substring is present in the specified string, otherwise false.
  2. not in - "not in" operator returns true if a character or entire substring does not exist in the specified string, otherwise false.

lets see the example :

str1="JavaNotes"
str2="Java"
str3="Notes"
str4="Users"

print('Exmple of in operator ::')
print(str2 in str1)
print(str3 in str1)
print(str4 in str1)
print()
print(str2 not in str1)
print(str3 not in str1)
print(str4 not in str1)

output :

Exmple of in operator ::

True
True
False


False
False
True

3. Python Relational Operators 

All the comparison (relational) operators i.e., (<,><=,>=,==,!=,<>) are also applicable for strings. The Strings are compared based on the ASCII value or Unicode(i.e., dictionary Order).


Example : 

print("Java"=="Java")
print("java">="Java")
print("A"<"a")

output :

True
True
True

Explanation:

The ASCII value of a is 97, b is 98, c is 99 and so on. The ASCII value of A is 65, B is 66, C is 67 and so on. The comparison between strings are done on the basis on ASCII value.

Python String Slice Notation

Python String slice can be defined as a substring which is the part of the string. Therefore further substring can be obtained from a string.


There can be many forms to slice a string, as string can be accessed or indexed from both the direction and hence string can also be sliced from both the directions.

Syntax of Slice Operator :

str[start : stop : step ]


other syntax of slice 

str[start : stop]  #items start through stop-1

str[start : ] #items start through the rest of the array

str[ : stop]  #items from the beginning through stop-1

str[ : ]  # a copy of the whole array

example :

s="Monty Python"

print(s[6:10])
print(s[-12:-7])
print(s[-1: :-1]) #reversed all string
print(s[2: 10: 2]) #step = 2
print(s[ : : -1]) #reversed all string
print(s[ : 5]) #from 0 to 4
print(s[3 : ]) #from 3 to end of the string
print(s[ : ]) #copy all strin

output :

Pyth
Monty
nohtyP ytnoM
nyPt
nohtyP ytnoM
Monty
ty Python
Monty Python

NOTE: Both the operands passed for concatenation must be of same type, else it will show an error.

No comments:

Post a Comment

how to implement YOLOv3 using Python and TensorFlow

Object Detection with YOLOv3 Introduction YOLOv3 (You Only Look Once version 3) is a real-time object detection algorithm that can detect ob...