Python Program to Split a String at First Instance of a Character

Files in Python:

Files are identified locations on the disc where associated data is stored. They’re used to keep data in non-volatile memory for a long time (e.g. hard disk).

We use files for future usage of the data by permanently saving it because Random Access Memory (RAM) is volatile (it loses its contents when the machine is turned off).

We must first open a file before we can read from or write to it. When we’re finished, it needs to be closed so that the file’s resources may be released.

As a result, a file operation in Python is performed in the following order:

  • Create a new file
  • You can either read or write (perform the operations)
  • Close the file.

Given a string and a character, the task is to split a given string at the first instance of a given character in Python.

Program to Split a String at First Instance of a Character in Python

Below is the full approach for splitting a given string at the first instance of a given character in Python.

Method #1: Using Slicing (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Give some random character as static input and store it in another variable.
  • Get the index of the first occurrence of the given character using the find() function and store it in another variable.
  • Print the string until the index-1 of the given character using slicing.
  • Print the string after the index+1 of the given character using slicing.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "hello btechgeeks" 
# Give some random character as static input and store it in another variable.
gvn_chr = 'l'

# Get the index of the first occurrence of the given character using the 
# find() function and store it in another variable.
indx = gvn_str.find(gvn_chr)  
# Print the string until the index-1 of the given character using slicing.
print('The first part of the given string = ', gvn_str[:indx]) 
# Print the string after the index+1 of the given character using slicing.
print('The second part of the given string = ', gvn_str[indx+1:])

Output:

The first part of the given string =  he
The second part of the given string =  lo btechgeeks

Method #2: Using Slicing (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give some random character as user input using the input() function and store it in another variable.
  • Get the index of the first occurrence of the given character using the find() function and store it in another variable.
  • Print the string until the index-1 of the given character using slicing.
  • Print the string after the index+1 of the given character using slicing.
  • The Exit of Program.

Below is the implementation:

# Give the string user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Give some random character user input using the input() function
# and store it in another variable.
gvn_chr = input("Enter some random character = ")

# Get the index of first occurence of the given character using the 
# find() function and store it in another variable.
indx = gvn_str.find(gvn_chr)  
# Print the string until the index-1 of the given character using slicing.
print('The first part of the given string = ', gvn_str[:indx]) 
# Print the string after the index+1 of the given character using slicing.
print('The second part of the given string = ', gvn_str[indx+1:])

Output:

Enter some random string = good morning btechgeeks
Enter some random character = n
The first part of the given string = good mor
The second part of the given string = ing btechgeeks

Must Read:

AUBANK Pivot Point Calculator