Python Program for Snake Case of a Given Sentence

In the previous article, we have discussed Python Program to Check Trimorphic Number or Not
The task is to remove spaces from a sentence and rewrite it in the Snake case. It is a writing style in which spaces are replaced with underscores and all words begin with small letters.

 The lower() method in Python:

lower() returns a string with all characters in lower case.

Examples:

Example1:

Input:

Given string/sentence = Hello this is Btechgeeks

Output:

The given sentence { Hello this is Btechgeeks } after Converting into Snake case :
hello_this_is_btechgeeks

Example2:

Input:

Given string/sentence = Good morning This is Btechgeeks

Output:

The given sentence { Good morning This is Btechgeeks } after Converting into Snake case :
good_morning_this_is_btechgeeks

Program for Snake Case of a Given Sentence in Python

Below are the ways to remove spaces from a sentence and rewrite it in the Snake case:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the string/sentence as static input and store it in a variable.
  • Calculate the length of the given string using the len() function and store it in another variable.
  • Convert the given string into a list of characters using the list() function and store it in another variable say lst_strng.
  • Iterate up to the length of the given string using the for loop.
  • Check if the element of the list of characters ( lst_strng ) is equal to space ( ‘ ‘ ) using the if conditional statement.
  • If the statement is true, then replace the space with the underscore( _ ).
  • If the statement is false, then convert the elements of the list of characters ( lst_strng ) into the lower case using the lower() function.
  • Convert the above list lst_strng into the string using the join function.
  • Store it in the same variable lst_strng.
  • Print the lst_strng to remove spaces from a given sentence and rewrite it in the Snake case.
  • The Exit of the Program.

Below is the implementation:

# Give the string/sentence as static input and store it in a variable.
gvn_strng = "Hello this is Btechgeeks"
# Calculate the length of the given string using the len() function and
# store it in another variable.
str_lengt = len(gvn_strng)
# Convert the given string into a list of characters using the list() function and
# store it in another variable say lst_strng.
lst_strng = list(gvn_strng)
# Iterate up to the length of the given string using the for loop.
print("The given sentence {", gvn_strng,
      "} after Converting into Snake case :")
for itr in range(str_lengt):
 # Check if the element of the list of characters ( lst_strng ) is equal to space ( ' ' )
    # using the if conditional statement.
    if (lst_strng[itr] == ' '):
      # If the statement is true, then replace the space with the underscore( _ ).
        lst_strng[itr] = '_'
    else:
        # If the statement is false, then convert the elements of the list of characters ( lst_strng )
        # into lower case using the lower() function.
        lst_strng[itr] = lst_strng[itr].lower()
# Convert the above list lst_strng into the string using the join function.
# Store it in the same variable lst_strng.
lst_strng = "".join(lst_strng)
# Print the lst_strng to remove spaces from a given sentence and rewrite it in the
# Snake case.
print(lst_strng)

Output:

The given sentence { Hello this is Btechgeeks } after Converting into Snake case :
hello_this_is_btechgeeks

Method #2: Using For loop (User Input)

Approach:

  • Give the string/sentence as user input using the input() function and store it in a variable.
  • Calculate the length of the given string using the len() function and store it in another variable.
  • Convert the given string into a list of characters using the list() function and store it in another variable say lst_strng.
  • Iterate up to the length of the given string using the for loop.
  • Check if the element of the list of characters ( lst_strng ) is equal to space ( ‘ ‘ ) using the if conditional statement.
  • If the statement is true, then replace the space with the underscore( _ ).
  • If the statement is false, then convert the elements of the list of characters ( lst_strng ) into the lower case using the lower() function.
  • Convert the above list lst_strng into the string using the join function.
  • Store it in the same variable lst_strng.
  • Print the lst_strng to remove spaces from a given sentence and rewrite it in the Snake case.
  • The Exit of the Program.

Below is the implementation:

# Give the string/sentence as user input using the input() function
# and store it in a variable.
gvn_strng = input("Enter some random string= ")
# Calculate the length of the given string using the len() function and
# store it in another variable.
str_lengt = len(gvn_strng)
# Convert the given string into a list of characters using the list() function and
# store it in another variable say lst_strng.
lst_strng = list(gvn_strng)
# Iterate up to the length of the given string using the for loop.
print("The given sentence {", gvn_strng,
      "} after Converting into Snake case :")
for itr in range(str_lengt):
 # Check if the element of the list of characters ( lst_strng ) is equal to space ( ' ' )
    # using the if conditional statement.
    if (lst_strng[itr] == ' '):
      # If the statement is true, then replace the space with the underscore( _ ).
        lst_strng[itr] = '_'
    else:
        # If the statement is false, then convert the elements of the list of characters ( lst_strng )
        # into lower case using the lower() function.
        lst_strng[itr] = lst_strng[itr].lower()
# Convert the above list lst_strng into the string using the join function.
# Store it in the same variable lst_strng.
lst_strng = "".join(lst_strng)
# Print the lst_strng to remove spaces from a given sentence and rewrite it in the
# Snake case.
print(lst_strng)

Output:

Enter some random string= Good morning This is Btechgeeks
The given sentence { Good morning This is Btechgeeks } after Converting into Snake case :
good_morning_this_is_btechgeeks

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.