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 -
Example 4: Copy the Entire String-
Example 5: Using Step -
text = "Python"
print(text[0:6:2])
text = "Python"
print(text[::2])
print(text[::-1])