Python Program to split a string at first instance of a character

Python Program to split a string at first instance of a character

In this article, we will be solving question based on Strings in Python. String is very important topic of Python and thus solving questions on it is very necessary for job interviews and competitions. According to the question we have to split a string at first instance of a character.

If you like the article content, then share it in your coding groups so that more people can take advantage of the content. So, without wasting any time let's solve this question.

Question - Write a Python Program to split a string at first instance of a character

Python Program to split a string at first instance of a character


Logic and Explanation

Question - Write a Python Program to split a string at first instance of a character

Python Program to split a string at first instance of a character - In this program we have to split a string at first instance of a character. So, let us consider a string "Python Programming" stored in a variable 's'. Now, there is a method in string "find" which returns the index of the first occurrence of the character in a string.

If you don't know about this method, then check out all the important String Methods in Python - Click Here

So, we will find the index of first occurrence of the character at which we want to split the string and then we will use string slicing to split the string. The first part of the string will be from index 0 to one index less than that of the character and second part will be from the index of character plus one to the end.

I hope the logic and the approach to the question we have taken will be clear to you. Here's the code for the same.

s = "Python Programming" 
k = input("Enter character at which you want to split") 

#It returns the index of first occurence of character k 
i = s.find(k)  

print('First Part:- ', s[:i]) 
print('Second Part:- ', s[i+1:])


Output

Enter character at which you want to splitn
First Part:-  Pytho
Second Part:-   Programming


Code Insights

  1. Find() is a built-in string method which returns the index of the first occurrence of the character.
  2. By writing [ : i], we mean that part of string from starting to one less than index i. Similarly, leaving blank at the end means till end of the string.

So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.

For More Such Python Programs - Click Here

Enjoy Coding!!!


Also Read :-

Extract Phone Number Details Using Python






For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post