Important Notice:

Slicing

Slicing

3 views 1 min read

 Slicing (स्लाइसिंग):-

Slicing (स्लाइसिंग) वह प्रक्रिया (Process) है जिसके द्वारा String (स्ट्रिंग) के एक भाग (Substring) को प्राप्त (Access) किया जाता है।

Indexing से हम एक समय में केवल एक Character प्राप्त कर सकते हैं, जबकि Slicing की सहायता से हम एक या एक से अधिक Characters (String का कोई भाग) प्राप्त कर सकते हैं।

Slicing में Start Index (प्रारंभिक इंडेक्स), Stop Index (अंतिम इंडेक्स) और Step (कदम) का उपयोग किया जाता है।

English

Slicing is the process of extracting a portion (substring) of a string.

With Indexing, we can access only one character at a time, whereas Slicing allows us to access one or more consecutive characters from a string.

Slicing uses Start Index, Stop Index, and Step values.

Syntax (सिंटैक्स) -

string_name[start : stop : step]

Start-  उस Index को दर्शाता है जहाँ से Slicing शुरू होती है, और यह हमेशा Result में शामिल (Included) होता है।

Stop-उस Index को दर्शाता है जहाँ तक Slicing चलती है, लेकिन stop Index Result में शामिल (Excluded) नहीं होता।

step- यह निर्धारित करता है कि Slicing के दौरान हर कितने Characters के अंतर (Gap) पर अगला Character चुना जाएगा।

Example 1: Basic Slicing -

text = "Python"

print(text[1:4])
 
Output -
yth
 
Example 2: From Beginning -
text = "Python"

print(text[:4])
 
Output -
Pyth
 
Example 3: Up to the End -
 
text = "Python"

print(text[2:])
 
Output -
thon

Example 4: Copy the Entire String-

text = "Python"
print(text[:])
 
Output -
Python

Example 5: Using Step -

text = "Python"

print(text[0:6:2])

Output -
Pto
Example 6: Skip Every Second Character-

text = "Python"

print(text[::2])

Output -
Pormig
 
Example 7: Reverse a String -
 
 text = "Python"

print(text[::-1])

 Output -
nohtyP
 

Related Notes