{"id":14784,"date":"2021-09-30T12:00:14","date_gmt":"2021-09-30T06:30:14","guid":{"rendered":"https:\/\/python-programs.com\/?p=14784"},"modified":"2021-11-22T18:35:26","modified_gmt":"2021-11-22T13:05:26","slug":"python-program-to-remove-the-first-occurrence-of-character-in-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-remove-the-first-occurrence-of-character-in-a-string\/","title":{"rendered":"Python Program to Remove the First Occurrence of Character in a String"},"content":{"rendered":"
In this article, we’ll learn how to use Python to remove the first occurrence of a character in a string. The aim is to remove the first occurrence of a character in a string. To begin, locate the character in the string and confirm that it is the first occurrence. Finally, remove the character from the string and display the result.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to remove the first occurrence of the given character in a string in Python.<\/p>\n Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners<\/a> tutorial.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n In this article, we’ll learn how to use Python to remove the first occurrence of a character in a string. The aim is to remove the first occurrence of a character in a string. To begin, locate the character in the string and confirm that it is the first occurrence. Finally, remove the character from …<\/p>\nGiven String =goodmorningthisisbtechgeekspython\r\nGiven Character =s<\/pre>\n
The Given string after removing first occurence of the character [ s ] is goodmorningthisisbtechgeekspython\r\nThe Given string after removing first occurence of the character [ s ] is goodmorningthiisbtechgeekspython<\/pre>\n
Given String =hellothisisbtechgeeks\r\nGiven Character =e<\/pre>\n
The Given string after removing first occurence of the character [ e ] is hellothisisbtechgeeks\r\nThe Given string after removing first occurence of the character [ e ] is hllothisisbtechgeeks<\/pre>\n
Program to Remove the First Occurrence of Character in a String in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvnstrng = 'hellothisisbtechgeeks'\r\n# Give the character as static input and store it in another variable.\r\ngvnchar = 'e'\r\n# Calculate the length of the string using the len() function.\r\nstrnglength = len(gvnstrng)\r\n# Loop in the characters of the string using the For loop.\r\nfor i in range(strnglength):\r\n # Check if the iterator character is equal to the given character\r\n # using the if conditional statement and == operator.\r\n if(gvnstrng[i] == gvnchar):\r\n # Initialize the resultstrng with the given string\r\n # from 0 to iterator value concatenated\r\n # with iterator value +1 to the length of the given string.\r\n reesultvalu = gvnstrng[0:i] + gvnstrng[i + 1:strnglength]\r\n # Break the For loop using the break keyword.\r\n break\r\n# Print the Result.\r\nprint(\r\n 'The Given string after removing first occurence of the character [', gvnchar, '] is', gvnstrng)\r\nprint(\r\n 'The Given string after removing first occurence of the character [', gvnchar, '] is', reesultvalu)\r\n<\/pre>\n
The Given string after removing first occurence of the character [ e ] is hellothisisbtechgeeks\r\nThe Given string after removing first occurence of the character [ e ] is hllothisisbtechgeeks<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvnstrng = input('Enter some random given string = ')\r\n# Give the character as user input using the input() function and store it in another variable.\r\ngvnchar = input('Enter some random character = ')\r\n# Calculate the length of the string using the len() function.\r\nstrnglength = len(gvnstrng)\r\n# Loop in the characters of the string using the For loop.\r\nfor i in range(strnglength):\r\n # Check if the iterator character is equal to the given character\r\n # using the if conditional statement and == operator.\r\n if(gvnstrng[i] == gvnchar):\r\n # Initialize the resultstrng with the given string\r\n # from 0 to iterator value concatenated\r\n # with iterator value +1 to the length of the given string.\r\n reesultvalu = gvnstrng[0:i] + gvnstrng[i + 1:strnglength]\r\n # Break the For loop using the break keyword.\r\n break\r\n# Print the Result.\r\nprint(\r\n 'The Given string after removing first occurence of the character [', gvnchar, '] is', gvnstrng)\r\nprint(\r\n 'The Given string after removing first occurence of the character [', gvnchar, '] is', reesultvalu)\r\n<\/pre>\n
Enter some random given string = goodmorningthisisbtechgeekspython\r\nEnter some random character = s\r\nThe Given string after removing first occurence of the character [ s ] is goodmorningthisisbtechgeekspython\r\nThe Given string after removing first occurence of the character [ s ] is goodmorningthiisbtechgeekspython<\/pre>\n
\n