Python mai hm string ka use krte hai text information ko store krne ke liye jaise ki name, jaisa ki hm jante hai Python mai character nhi hota python mai character ko string of length one mana jata hai.
String ko single quotation mai bhi represent kr skte hai aur double quotation mai bhi represent kr skte hai. Let's see an example..
print('hey')
print("hey")
agar hame multiline string ko assign krna hai to ham wo bhi kr skte hai
example:
a = """hey, this is rishabh gupta
and I will teach you python from scratch
enjoy the blog"""
print(a)
print(a)
output
hey, this is rishabh gupta
and I will teach you python from scratch
enjoy the blog
Python ki string ek array jaise hoti hai iska mtlb ye hua ki hm python string ki indexing kr skte hai (indexing krne ka mtlb hua position dena) , aur us string se hm koi bhi letter nikal skte hai , aaiye isko aur clear banane ke liye example ka sahara lete hai.
mystr="rishabh is good boy"
print(mystr)
rishabh is good boy
"ye baat hamesa yaad rakhiyega ki python follows the zero based index system jiska mtlb hai ki python m indexing
zero se hoti hai , to hm hamesha zero se count karenge one se nhi "
"Python mai space bhi count hota hai"
print(mystr[4])
a
ap dekh skte hai ki 4th position mai a hai [0-r ,1-i,2-s,3-h,4-a,5-b,6-h,7-"space",8-i,and so on]
aur example ki madad se python string ko clear samjhte hai
----------------------------------------------------------------------
print(mystr[0:18])
rishabh is good bo
# 0:18 mai 0 include hota hai lekin 18 exclude hota hai yani ki is example mai 18th position ko nhi gina jayega
------------------------------------------------------------------
print(mystr[0:19])rishabh is good boy
#isme 19 exclude hai mtlb 18th position tak gina jayega aur usme "rishabh is good boy" pur complete sentence aata hai
-------------------------------------------------------------------------
print(mystr[0:19:2])rsahi odby
#isme hm actually ek character ko skip kra rhe hai (skipping one character)#:2 ka mtlb hai ki ek character chhod ke print kro agar :3 hota mtlb 2 character ko chhod ke print kro# isliye ap dekh skte hai ki r ke baad i skip ho gya hai aur s hai , s ke baad h skip ho gya aur a hai and so on.....------------------------------------------------------------------------------------------------print(mystr[::])
rishabh is good boy
#ye ek dusra tarika hai string print karane ka , agar man lo apko number count krna nhi aata hai ya fir apko number count krna bahut alasya wala kaam lagta hai to ap is tarah bhi likh skte hai.-----------------------------------------------------------------------------------------------------print(mystr[::-1])yob doog si hbahsir
#-1 basically string ko ulta kr deta hai ,to isme piche se character print hona start ho gye
#dekhiye ye sb chije apko manually remember krni hogi
-------------------------------------------------------------------------------------String Methods
print(mystr.isalnum())False
#isme hame output false mila hai , iska mtlb ye hai ki ye jo mystr string ye alpha numeric nhi hai mtlb is string ke andar space hai
#ham dekh skte hai ki "rishabh" ke baad space hai fir "is" hai fir space hai..., to ye string ke andar space hai is liye hame false result mil raha hai
----------------------------------------------------------------------------------
rig="jaijavanjaikisan"
#yaha hamne ek dusri string li hai aur ab ham isme operation krenge
-------------------------------------------------------------------------------
print(rig.isalnum())
True
#to ab hm dekh skte hai ki is string mai space nhi hai , isliye hame result true mil raha hai.
----------------------------------------------------------------------------------------------
print(rig.isalpha())True
#is alpha check krta hai ki string purely alphabet hai ki nhi , aur hamari string purely alphabet hai isliye hame result true mila hai
-------------------------------------------------------------------------
print(mystr.endswith("boy"))True
#mystr boy se end ho raha hai isliye it return true
-----------------------------------------------------------------------------
print(mystr.count("b"))2
#count function string ko count krne ke liye hai, mystr mai 2 b hai, isliye hame 2 return hua
-------------------------------------------------------------------------------
print(mystr.count("k"))0
#0 "k" hai mystr mai
--------------------------------------------------------------------------
print(mystr.capitalize())Rishabh is good boy
#capitalize function string ko capatilize kr deta hai, yani string ke pahle character ko capital letter mai convert kr deta hai.
------------------------------------------------------------------------------------------
pk="Ziddi hu MAI"
print(pk.lower())ziddi hu mai
#convert to lower case
-----------------------------------------------------------------------------
sk="pagal hu mai"
print(sk.upper())PAGAL HU MAI
#convert to upper case
--------------------------------------------------------------------------------
print(mystr.replace("good" , "bad"))rishabh is bad boy
#as a name suggest replace function are used to replace the string
-----------------------------------------------------------------
Python mai kafi saare string function hai maine kuch hi mention kiye hai , ap python ki original documentaion mai jake check kr skte haihttps://docs.python.org/2/library/string.htmlBonus:----
Method | Description |
---|---|
capitalize() | Converts the first character to upper case |
casefold() | Converts string into lower case |
center() | Returns a centered string |
count() | Returns the number of times a specified value occurs in a string |
encode() | Returns an encoded version of the string |
endswith() | Returns true if the string ends with the specified value |
expandtabs() | Sets the tab size of the string |
find() | Searches the string for a specified value and returns the position of where it was found |
format() | Formats specified values in a string |
format_map() | Formats specified values in a string |
index() | Searches the string for a specified value and returns the position of where it was found |
isalnum() | Returns True if all characters in the string are alphanumeric |
isalpha() | Returns True if all characters in the string are in the alphabet |
isdecimal() | Returns True if all characters in the string are decimals |
isdigit() | Returns True if all characters in the string are digits |
isidentifier() | Returns True if the string is an identifier |
islower() | Returns True if all characters in the string are lower case |
isnumeric() | Returns True if all characters in the string are numeric |
isprintable() | Returns True if all characters in the string are printable |
isspace() | Returns True if all characters in the string are whitespaces |
istitle() | Returns True if the string follows the rules of a title |
isupper() | Returns True if all characters in the string are upper case |
join() | Joins the elements of an iterable to the end of the string |
ljust() | Returns a left justified version of the string |
lower() | Converts a string into lower case |
lstrip() | Returns a left trim version of the string |
maketrans() | Returns a translation table to be used in translations |
partition() | Returns a tuple where the string is parted into three parts |
replace() | Returns a string where a specified value is replaced with a specified value |
rfind() | Searches the string for a specified value and returns the last position of where it was found |
rindex() | Searches the string for a specified value and returns the last position of where it was found |
rjust() | Returns a right justified version of the string |
rpartition() | Returns a tuple where the string is parted into three parts |
rsplit() | Splits the string at the specified separator, and returns a list |
rstrip() | Returns a right trim version of the string |
split() | Splits the string at the specified separator, and returns a list |
splitlines() | Splits the string at line breaks and returns a list |
startswith() | Returns true if the string starts with the specified value |
strip() | Returns a trimmed version of the string |
swapcase() | Swaps cases, lower case becomes upper case and vice versa |
title() | Converts the first character of each word to upper case |
translate() | Returns a translated string |
upper() | Converts a string into upper case |
zfill() | Fills the string with a specified number of 0 values at the beginning |
he
Sir kafi shai blog he
ReplyDelete😂
Delete