Python | Data Types | String & it's Conversion By. Shivansh Sir
PYTHON PROGRAMMING
PYTHON | DATA TYPES | STRING
String ये characters का set होता है | characters; letters, numbers या special symbols हो सकते है | Python में single(' ') या double(" ") quotes के अन्दर लिखे जाते है | String ये immutable data type है | String ये Common Data Type है | ये data type सामान्यतः सभी Computer Languages में पाया जाता है | String ये एक से ज्यादा characters का sequence होता है |
Source Code
str1 = "Welcome to Sunrise Computer"
str2 = "Learn Programming By Shivansh Sir"
print(str1)
print(str2)
Output
Welcome to Sunrise Computer
Learn Programming By Shivansh Sir
Notes :- Programming languages में string को index में print किया जाता है उसी प्रकार से Python में भी string को indexes से print किया जाता है | string के सबसे पहले character को index '0' से शुरू होती है और string के सबसे आखिरी index '-1' होती है |
str = "Welcome to Sunrise Computer"
print("First letter in string :", str[0])
print("Last letter in string :", str[-1])
print("Second last letter in string :", str[-2])
Output
First letter in string : W
Last letter in string : r
Second last letter in string : e
PYTHON | DATA TYPES | STRING NOTATION
Python में String को single quotes(' ') या double quotes(" ") में लिखा जाता है |
Example
'Enclosing String in single quotes'
"Enclosing String in double quotes"
Example for String
Source Code
var str = "Welcome to Sunrise Computer"
print(str)
Output
Welcome to Sunrise Computer
PYTHON | DATA TYPES | STRING INDEX
String की index '0' से शुरू होता है और आखिरी index '-1' होता है |
Source Code
str = "Welcome to Sunrise Computer"
print(str[0])
print(str[-1])
Output
W
r
PYTHON | DATA TYPES | CREATING SUBSTRING
Python में string से substring को create करना हो तो square bracket([]) में colon(:) का इस्तेमाल किया जाता है |
Syntax for Creating Substring
सिर्फ starting index दिया जाता है तो startIndex से पूरा string display किया जाता है |
str[start_Index : end_Index(Optional default '-1')]
सिर्फ ending index दिया जाता है तो 0th index से endIndex तक string display किया जाता है |
str[start_Index(Optional default '0') : end_Index]
Source Code
str = "Welcome to Sunrise Computer"
print(str[3:])
print(str[:8])
print(str[3:8])
Output
come to Sunrise Computer Welcome come
PYTHON | DATA TYPES | STRING CONCATENATION
एक string को दुसरे string से जोड़ने के लिए '+' Operator का इस्तेमाल किया जाता है |
Source Code
str1 = "Welcome to Sunrise Computer"
str2 = "Learn Python By. Shivansh Sir"
str3 = str1,str2
print(str3)
Output
'Welcome to Sunrise Computer', 'Learn Python By. Shivansh Sir'
print(str1,str2)
Output
Welcome to Sunrise Computer Learn Python By. Shivansh Sir
print(str1+str2)
Output
Welcome to Sunrise ComputerLearn Python By. Shivansh Sir
For Any Doubt Clear on Telegram Discussion Group
Join Us On Social MediaFor Any Query WhatsApp Now
Comments
Post a Comment