{"id":19046,"date":"2021-08-29T15:13:19","date_gmt":"2021-08-29T09:43:19","guid":{"rendered":"https:\/\/python-programs.com\/?p=19046"},"modified":"2021-11-22T18:37:09","modified_gmt":"2021-11-22T13:07:09","slug":"python-program-for-sign-change","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-sign-change\/","title":{"rendered":"Python Program for Sign Change"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find the Previous Armstrong Number<\/a> 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 ways to change the ‘+’ sign into ‘-‘ and vice versa for a given string\u00a0 :<\/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 Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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 …<\/p>\n
\nGiven a String and the task is to change the ‘+’ sign into ‘-‘ and vice versa for a given string.<\/p>\nGiven String = \" --btech++geeks-- \"<\/pre>\n
The given string { --btech++geeks-- } after changing the '+' sign into '-' and vice versa = ++btech--geeks++<\/pre>\n
Given String = \"--1++2--3+\"<\/pre>\n
The given string { --1++2--3+ } after changing the '+' sign into '-' and vice versa = ++1--2++3-<\/pre>\n
Program for Sign Change 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\ngvn_str = \"32--1++4\"\r\n# Take an empty string say \"new_str\" and store it in another variable.\r\nnew_str = \"\"\r\n# Loop in the given string using the for loop.\r\nfor itr in gvn_str:\r\n # Inside the loop, check if the iterator value is equal to '+' sign using the if\r\n # conditional statement.\r\n if(itr == \"+\"):\r\n # If it is true, then concatenate the\u00a0 '-' sign with the new_str and store it in the\r\n # same variable new_str.\r\n new_str += \"-\"\r\n# Check if the iterator value is equal to the '-' sign using the elif conditional statement.\r\n elif(itr == \"-\"):\r\n # If it is true, then concatenate the\u00a0 '+' sign with the new_str and store it in the\r\n # same variable new_str.\r\n new_str += \"+\"\r\n else:\r\n # Else concatenate the iterator value to the new_str and store it in the same variable new_str.\r\n new_str += itr\r\n# Print the given string after changing the '+' sign into '-' and vice versa.\r\nprint(\"The given string {\", gvn_str,\r\n \"} after changing the '+' sign into '-' and vice versa =\", new_str)\r\n<\/pre>\n
The given string { 32--1++4 } after changing the '+' sign into '-' and vice versa = 32++1--4<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and\r\n# store it in a variable.\r\ngvn_str = input(\"Enter some random String = \")\r\n# Take an empty string say \"new_str\" and store it in another variable.\r\nnew_str = \"\"\r\n# Loop in the given string using the for loop.\r\nfor itr in gvn_str:\r\n # Inside the loop, check if the iterator value is equal to '+' sign using the if\r\n # conditional statement.\r\n if(itr == \"+\"):\r\n # If it is true, then concatenate the\u00a0 '-' sign with the new_str and store it in the\r\n # same variable new_str.\r\n new_str += \"-\"\r\n# Check if the iterator value is equal to the '-' sign using the elif conditional statement.\r\n elif(itr == \"-\"):\r\n # If it is true, then concatenate the\u00a0 '+' sign with the new_str and store it in the\r\n # same variable new_str.\r\n new_str += \"+\"\r\n else:\r\n # Else concatenate the iterator value to the new_str and store it in the same variable new_str.\r\n new_str += itr\r\n# Print the given string after changing the '+' sign into '-' and vice versa.\r\nprint(\"The given string {\", gvn_str,\r\n \"} after changing the '+' sign into '-' and vice versa =\", new_str)\r\n<\/pre>\n
Enter some random String = btech++--geeks+++--\r\nThe given string { btech++--geeks+++-- } after changing the '+' sign into '-' and vice versa = btech--++geeks---++<\/pre>\n
\n