Header Ads Widget

riti-advertiesment

Python String-Slicing, Indexing, Concatenation, other operations on Strings

 Python Strings 

(Slicing, Indexing, Concatenation, other operations on Strings)



पायथन में स्ट्रिंग्स या तो   single quotation marks, या   double quotation marks चिह्नों से घिरे होते हैं।  एक स्ट्रिंग वेरिएबल  में एक शब्द या एक से अधिक वाक्य हो सकते हैं।

वैर="R-ITI.com एक शैक्षिक वेबसाइट है."

print (वैर)

Multiline String

str1=''' 

R-ITI.com is education website.

Develop my R-ITI Team in 2012.

'''

print (str1)

Output

R-ITI.com is education website.

Develop my R-ITI Team in 2012

Strings in Arrays

a = "Welcome, India!"
print(a[1])

Output
Welcome

Strings in Loop

for x in "banana":
  print(x) 

Output
b
a
n
a
n
a

Strings in Loop

var = "Hello, RITI!"
print(len(var))

Output
12

Check String

txt = "The best things in life are free!"
print("free" in txt)
Output
True

txt = "The best things in life are free!"
if "free" in txt:
  print("Yes, 'free' is present.")

Check if NOT

txt = "The best things in life are free!"
print("expensive" not in txt)

txt = "The best things in life are free!"
if "expensive" not in txt:
  print("Yes, 'expensive' is NOT present.")

Python Slice String

आप slice syntax का उपयोग करके स्ट्रिंग के कुछ या पूरा भाग को  वापस ला सकते हैं.
निगेटिव इनडेक्स का प्रयोग slice String में अन्त से शुरु करते है।

b = "Hello, World!"
print(b[2:5])
print(b[:5])
print(b[2:])
print(b[-5:-1])

Output
llo
Hello
llo, World!
orld

Python Index String

The index() method finds the first occurrence of the specified value. The index() method raises an exception if the value is not found.
The index() method is almost the same as the find() method, the only difference is that the find() method returns -1 if the value is not found. (See example below)

txt = "Hello, welcome to my world."
x = txt.index("welcome")
print(x)
Output
7

Python Concatenation String

a = "R-ITI"
b = ".com"
c = a + b
print(c)
Output
R-ITI.com


Web Designing Course