For Enquiry: 93450 45466

SubString In Python



Here is what we have on this page today.
We know that a string is a sequence of characters, but today we will learn how to get a part of any string, known as substring.

Checkout this completeย Online Python Training by FITA. FITA provides a complete Python course where you will be building real-time projects like Bitly and Twitter bundled with Django, placement support and certification at an affordable price.

String Slicing

String slicing can be done using the [] (square brackets) notation, just as you get index.
Slice for a character

The string[index] will return a single character of your string at the index.Since the indexing starts at 0, the string[0] will give you the very first character and the string[-1] will give the very last character.And the rest of the characters can be found using their position

my_string = โ€˜Get a complete Python tutorial at FITAโ€™   print(my_string[0])   print(my_string[len(my_string)//2])   print(my_string[-1])
And the above code will output..
G o A
You can get the training even if you are not in GOA.
Slicing characters starting at n.
The string[first_index:] will give you all the characters starting from the index given.
print(my_string[6:]) print(my_string[6::])
This will give give you all the characters except for the first 6,both the lines will give you the same output as
complete Python tutorial At FITA
Slicing characters for first n characters.
The string[:end_index] will give all the characters until the given index.
my_string =’Get a complete Python tutorial At FITA’   print(my_string[:6])
This will give you the first 6 characters of the my_string.
Get a
Slicing characters with a jump or step.
The string[::step] will give all the characters with a step or skip of the the value For example
my_string =’Get a complete Python tutorial At FITA’   print(my_string[::11])
This will give you characters starting from 0 till the end but with a skip of 11 characters in between.Here is the output..
Get
Note that this is not the first 3 characters, let me give another example
print(my_string[::25])
Output
Go
Now let’s fill all the gaps with a start ,end, step.
Slice characters from start to end with a step.
The my_string[start:end:step] will give you the characters from starting position to the end position with a skip of step characters.
my_string =’Get a complete Python tutorial At FITA’   print(my_string[4:35:12]) print(my_string[9::4]) print(my_string[:24:10]) print(my_string[9:18:4])
Here is the output for the above
aya pet ol A Gln pet
Negative indexing
A negative index will mean that you start from the end i.e., from right to left.
print(my_string[-2]) # T
will give the last second character.
print(my_string[-6:]) # t FITA
will give last 6 characters
print(my_string[6:-6]) # complete Python tutorial
Will give all characters except the first 6 and last 6 characters.

find() method

The find() is a built-in method in python which will return the starting index of the substring in the string, else return -1 if it’s not present. For instance,
s = ‘Get a complete Python tutorial At FITA’   print(s.find(‘FITA’))
Will give you 34.

Check for a substring

You can also use the in or not in keywords to check if a substring is present in the string, which will give a boolean result.
my_string = ‘Get a complete Python tutorial At FITA’   print(‘ATUFA’ not in my_string) print(‘complete’ in my_string)
Which outputs..
True True
Also you can count the number of occurrences of the substring , when you have checked if it is present, else it will return 0.
my_string = ‘Get a complete Python tutorial At FITA’   print(my_string.count(‘FIT’)) print(my_string.count(‘a’))
Output
1 4

Splitting Strings

You can split a string at a particular substring and simultaneously create a list of all the remaining substrings excluding split substring using the .split() method. Here’s an example of the split method.
my_string = ‘Get a complete Python tutorial At FITA’   print(my_string.split(‘ ‘)) print(my_string.split(‘P’))
In the first print statement I am splitting at space, and in the second one at P. Here’s the output
[‘Get’, ‘a’, ‘complete’, ‘Python’, ‘tutorial’, ‘At’, ‘FITA’] [‘Get a complete ‘, ‘ython tutorial At FITA’]
You can now even join these substrings using the .join() method.

This was all about slicing strings in python with its implementation..To get in-depth knowledge of Python along with its various applications and real-time projects, you can enroll in Python Training in Chennaiย or Python Training in Bangaloreย by FITA at an affordable price, which includes real time projects with certification, support and career guidance assistance.





  • Trending Courses

    JAVA Training In Chennai Software Testing Training In Chennai Selenium Training In Chennai Python Training in Chennai Data Science Course In Chennai Digital Marketing Course In Chennai DevOps Training In Chennai German Classes In Chennai Artificial Intelligence Course in Chennai AWS Training in Chennai UI UX Design course in Chennai Tally course in Chennai Full Stack Developer course in Chennai Salesforce Training in Chennai ReactJS Training in Chennai CCNA course in Chennai Ethical Hacking course in Chennai RPA Training In Chennai Cyber Security Course in Chennai IELTS Coaching in Chennai Graphic Design Courses in Chennai Spoken English Classes in Chennai Data Analytics Course in Chennai

    Spring Training in Chennai Struts Training in Chennai Web Designing Course In Chennai Android Training In Chennai AngularJS Training in Chennai Dot Net Training In Chennai C / C++ Training In Chennai Django Training in Chennai PHP Training In Chennai iOS Training In Chennai SEO Training In Chennai Oracle Training In Chennai Cloud Computing Training In Chennai Big Data Hadoop Training In Chennai UNIX Training In Chennai Core Java Training in Chennai Placement Training In Chennai Javascript Training in Chennai Hibernate Training in Chennai HTML5 Training in Chennai Photoshop Classes in Chennai Mobile Testing Training in Chennai QTP Training in Chennai LoadRunner Training in Chennai Drupal Training in Chennai Manual Testing Training in Chennai WordPress Training in Chennai SAS Training in Chennai Clinical SAS Training in Chennai Blue Prism Training in Chennai Machine Learning course in Chennai Microsoft Azure Training in Chennai Selenium with Python Training in Chennai UiPath Training in Chennai Microsoft Dynamics CRM Training in Chennai VMware Training in Chennai R Training in Chennai Automation Anywhere Training in Chennai GST Training in Chennai Spanish Classes in Chennai Japanese Classes in Chennai TOEFL Coaching in Chennai French Classes in Chennai Informatica Training in Chennai Informatica MDM Training in Chennai Big Data Analytics courses in Chennai Hadoop Admin Training in Chennai Blockchain Training in Chennai Ionic Training in Chennai IoT Training in Chennai Xamarin Training In Chennai Node JS Training In Chennai Content Writing Course in Chennai Advanced Excel Training In Chennai Corporate Training in Chennai Embedded Training In Chennai Linux Training In Chennai Oracle DBA Training In Chennai PEGA Training In Chennai Primavera Training In Chennai Tableau Training In Chennai Spark Training In Chennai Appium Training In Chennai Soft Skills Training In Chennai JMeter Training In Chennai Power BI Training In Chennai Social Media Marketing Courses In Chennai Talend Training in Chennai HR Courses in Chennai Google Cloud Training in Chennai SQL Training In Chennai CCNP Training in Chennai PMP Training in Chennai OET Coaching Centre in Chennai Business Analytics Course in Chennai NextJS Course in Chennai Vue JS Course in Chennai

  • Read More Read less
  • Are You Located in Any of these Areas

    Adambakkam, Adyar, Akkarai, Alandur, Alapakkam, Alwarpet, Alwarthirunagar, Ambattur, Ambattur Industrial Estate, Aminjikarai, Anakaputhur, Anna Nagar, Anna Salai, Arumbakkam, Ashok Nagar, Avadi, Ayanavaram, Besant Nagar, Bharathi Nagar, Camp Road, Cenotaph Road, Central, Chetpet, Chintadripet, Chitlapakkam, Chengalpattu, Choolaimedu, Chromepet, CIT Nagar, ECR, Eechankaranai, Egattur, Egmore, Ekkatuthangal, Gerugambakkam, Gopalapuram, Guduvanchery, Guindy, Injambakkam, Irumbuliyur, Iyyappanthangal, Jafferkhanpet, Jalladianpet, Kanathur, Kanchipuram, Kandhanchavadi, Kandigai, Karapakkam, Kasturbai Nagar, Kattankulathur, Kattupakkam, Kazhipattur, Keelkattalai, Kelambakkam, Kilpauk, KK Nagar, Kodambakkam, Kolapakkam, Kolathur, Kottivakkam, Kotturpuram, Kovalam, Kovilambakkam, Kovilanchery, Koyambedu, Kumananchavadi, Kundrathur, Little Mount, Madambakkam, Madhavaram, Madipakkam, Maduravoyal, Mahabalipuram, Mambakkam, Manapakkam, Mandaveli, Mangadu, Mannivakkam, Maraimalai Nagar, Medavakkam, Meenambakkam, Mogappair, Moolakadai, Moulivakkam, Mount Road, MRC Nagar, Mudichur, Mugalivakkam, Muttukadu, Mylapore, Nandambakkam, Nandanam, Nanganallur, Nanmangalam, Narayanapuram, Navalur, Neelankarai, Nesapakkam, Nolambur, Nungambakkam, OMR, Oragadam, Ottiyambakkam, Padappai, Padi, Padur, Palavakkam, Pallavan Salai, Pallavaram, Pallikaranai, Pammal, Parangimalai, Paruthipattu, Pazhavanthangal, Perambur, Perumbakkam, Perungudi, Polichalur, Pondy Bazaar, Ponmar, Poonamallee, Porur, Pudupakkam, Pudupet, Purasaiwakkam, Puzhuthivakkam, RA Puram, Rajakilpakkam, Ramapuram, Red Hills, Royapettah, Saidapet, Saidapet East, Saligramam, Sanatorium, Santhome, Santhosapuram, Selaiyur, Sembakkam, Semmanjeri, Shenoy Nagar, Sholinganallur, Singaperumal Koil, Siruseri, Sithalapakkam, Srinivasa Nagar, St Thomas Mount, T Nagar, Tambaram, Tambaram East, Taramani, Teynampet, Thalambur, Thirumangalam, Thirumazhisai, Thiruneermalai, Thiruvallur, Thiruvanmiyur, Thiruverkadu, Thiruvottiyur, Thoraipakkam, Thousand Light, Tidel Park, Tiruvallur, Triplicane, TTK Road, Ullagaram, Urapakkam, Uthandi, Vadapalani, Vadapalani East, Valasaravakkam, Vallalar Nagar, Valluvar Kottam, Vanagaram, Vandalur, Vasanta Nagar, Velachery, Vengaivasal, Vepery, Vettuvankeni, Vijaya Nagar, Villivakkam, Virugambakkam, West Mambalam, West Saidapet

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur or Pallikaranai branch is just few kilometre away from your location. If you need the best training in Chennai, driving a couple of extra kilometres is worth it!