In the previous article, we have discussed Python Program to Find the Previous Armstrong Number
Given a String and the task is to change the ‘+’ sign into ‘-‘ and vice versa for a given string.
Examples:
Example1:
Input:
Given String = " --btech++geeks-- "
Output:
The given string { --btech++geeks-- } after changing the '+' sign into '-' and vice versa = ++btech--geeks++
Example2:
Input:
Given String = "--1++2--3+"
Output:
The given string { --1++2--3+ } after changing the '+' sign into '-' and vice versa = ++1--2++3-
Program for Sign Change in Python
Below are ways to change the ‘+’ sign into ‘-‘ and vice versa for a given string :
Method #1: Using For Loop (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Take an empty string say “new_str” and store it in another variable.
- Loop in the given string using the for loop.
- Inside the loop, check if the iterator value is equal to ‘+’ sign using the if conditional statement.
- If it is true, then concatenate the ‘-‘ sign with the new_str and store it in the same variable new_str.
- Check if the iterator value is equal to the ‘-‘ sign using the elif conditional statement.
- If it is true, then concatenate the ‘+’ sign with the new_str and store it in the same variable new_str.
- Else concatenate the iterator value to the new_str and store it in the same variable new_str.
- Print the given string after changing the ‘+’ sign into ‘-‘ and vice versa.
- The Exit of the program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvn_str = "32--1++4" # Take an empty string say "new_str" and store it in another variable. new_str = "" # Loop in the given string using the for loop. for itr in gvn_str: # Inside the loop, check if the iterator value is equal to '+' sign using the if # conditional statement. if(itr == "+"): # If it is true, then concatenate the '-' sign with the new_str and store it in the # same variable new_str. new_str += "-" # Check if the iterator value is equal to the '-' sign using the elif conditional statement. elif(itr == "-"): # If it is true, then concatenate the '+' sign with the new_str and store it in the # same variable new_str. new_str += "+" else: # Else concatenate the iterator value to the new_str and store it in the same variable new_str. new_str += itr # Print the given string after changing the '+' sign into '-' and vice versa. print("The given string {", gvn_str, "} after changing the '+' sign into '-' and vice versa =", new_str)
Output:
The given string { 32--1++4 } after changing the '+' sign into '-' and vice versa = 32++1--4
Method #2: Using For loop (User Input)
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Take an empty string say “new_str” and store it in another variable.
- Loop in the given string using the for loop.
- Inside the loop, check if the iterator value is equal to ‘+’ sign using the if conditional statement.
- If it is true, then concatenate the ‘-‘ sign with the new_str and store it in the same variable new_str.
- Check if the iterator value is equal to the ‘-‘ sign using the elif conditional statement.
- If it is true, then concatenate the ‘+’ sign with the new_str and store it in the same variable new_str.
- Else concatenate the iterator value to the new_str and store it in the same variable new_str.
- Print the given string after changing the ‘+’ sign into ‘-‘ and vice versa.
- The Exit of the program.
Below is the implementation:
# Give the string as user input using the input() function and # store it in a variable. gvn_str = input("Enter some random String = ") # Take an empty string say "new_str" and store it in another variable. new_str = "" # Loop in the given string using the for loop. for itr in gvn_str: # Inside the loop, check if the iterator value is equal to '+' sign using the if # conditional statement. if(itr == "+"): # If it is true, then concatenate the '-' sign with the new_str and store it in the # same variable new_str. new_str += "-" # Check if the iterator value is equal to the '-' sign using the elif conditional statement. elif(itr == "-"): # If it is true, then concatenate the '+' sign with the new_str and store it in the # same variable new_str. new_str += "+" else: # Else concatenate the iterator value to the new_str and store it in the same variable new_str. new_str += itr # Print the given string after changing the '+' sign into '-' and vice versa. print("The given string {", gvn_str, "} after changing the '+' sign into '-' and vice versa =", new_str)
Output:
Enter some random String = btech++--geeks+++-- The given string { btech++--geeks+++-- } after changing the '+' sign into '-' and vice versa = btech--++geeks---++
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.