Python me String Literals

Python me String Literals ko Single Quotes ya fir Double Quotes ke andar likha jata hai, for example – ‘world’ or “world”. 

Agar koi number bhi inn single quotes aur double quotes ke andar aata hai to wo bhi ek string literal ki tarah hi count kiya jata hai, jaise – “23.5” , ‘4’ etc.

Variables me string kaise assign karte hain?

Variable me string assign karne ka simple funda hai, ek variable define kariye aur fir assignment operator ka use karke kisi bhi alphanumeric characters, words or sentence ko single quotes ya fir double quotes ke andar define kar dijiye.

x = “I am defined in Double Quotes”
y = ‘
I am defined in Single Quotes‘ 

print(x)
print(y)

Python me Multiline String kya hota hai?

Python me multiline string ko three time single quotes or double quotes ke sath likha jata hai. Example se samajhte hain:

x = “””I am defined in three times
double quotes to represent
multiline string in python“””

y = ”’I am defined in three times
single quotes to represent
multiline string in python

print(x)
print(y)

Python String aur Array me Difference

Python me strings array of bytes ke jaisa hota hai jo  uske unicode characters ko represent karta hai. Kyuki python ke case me koi bhi ‘char’ data type nhi hota hai issliye yaha par har ek unicode character string hi count hota hai.

a = “This is string”
b = a[3]
print (b)
print(type(b))

* Indexing hamesha ‘0’ se count shuru hogi

Python me Slicing ka concept

Python me slicing bhut hi jyada use hone wala topic hai. Slicing me start index aur end index hota hai jo colon (:) se seperated hota hai, jisse hum apne set kiye gaye range ki string ko return kar sakte hain. Example se samajhte hain:  

z = “Python, Slicing!”
print(z[8:15])

O/P : ‘Slicing’

* Yaha start index ki value count hogi lekin end index ki value count nhi hogi, balki (end index-1) tak ki value count hogi… Iska matlab ki upar diya code run hone ke baad “Slicing” print karega jaha ‘S’ ka index 8 hai aur ‘g’ ka index 14.

Python me Negative Indexing

Upar diya gaya concept Positive Indexing ka tha jaha hum index ki values positive de rahe the lekin negative indexing me hum index ki value ‘-‘ sign se represent karenge.

Positive indexing me indexing left to right hogi lekin negative indexing me indexing right to left hogi.Example se samajhte hain jaha hum ‘Slicing’ ko negative indexing ka use karke print karayenge.

z = “Python, Slicing!”
print(z[-8:-1])

O/P : ‘Slicing’

* Upar diye hue example se samajh aa raha h ki yaha hum right to left indexing count karenge, joki -1 se start hoga… jaha ‘!’ ka index -1, ‘g’ ka -2, ‘n’ ka -3 …. ‘S’ ka -8. To ‘Slicing’ ko print karane ke liye hume -8 ko start index aur -1 ko end index lena hoga.

Python me String Length

Python me string length ko calculate karne ke liye len() function ka use kiya jata hai.Jaha ye function kisi bhi string ke length ko return karta hai

s = “String, Length!”
print (len(s))

O/P : 15

Python me String Concatenation

Python me string Concatenation ka matlab alag-alag strings ko aapas me jodna. Example se samajhte hain: 

s1 = “Hello “
s2 = “Python”
s3 = s1 + s2
print (s3)

O/P : ‘Hello Python’

Python Check String

Kisi bhi string me kisi particular text ya string ke presence ko check karna: 

text = “We are studying about python string”
a = “string” in text
print(a)

O/P : True

text = “We are studying about python string”
n = “string” not in text
print(n)

O/P : False

Python String Methods

capitalize() 
casefold() 
center() 
count() 
encode() 
endswith() 
expandtabs() 
find() 
format() 
format_map() 
index()
isalnum() 
isalpha() 
isdecimal() 
isdigit()
isidentifier() 
islower() 
isnumeric() 
isprintable() 
isspace() 
istitle() 
isupper() 

join() 
ljust()
lower() 
lstrip() 
maketrans() 
partition() 
replace() 
rfind() 
rindex() 
rjust() 
rpartition() 
rsplit() 
rstrip() 
split() 
splitlines() 
startswith() 
strip() 
swapcase() 
title() 
translate() 
upper() 
zfill()