{"id":10226,"date":"2021-09-30T12:00:55","date_gmt":"2021-09-30T06:30:55","guid":{"rendered":"https:\/\/python-programs.com\/?p=10226"},"modified":"2021-11-22T18:34:39","modified_gmt":"2021-11-22T13:04:39","slug":"python-program-to-take-in-two-strings-and-display-the-larger-string-without-using-built-in-functions","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-take-in-two-strings-and-display-the-larger-string-without-using-built-in-functions\/","title":{"rendered":"Python Program to Take in Two Strings and Display the Larger String without Using Built-in Functions"},"content":{"rendered":"
Strings in Python:<\/strong><\/p>\n In Python, a string\u00a0may be a\u00a0<\/span>sequence of characters. It is\u00a0an information<\/span>\u00a0type that has been derived. Strings are unchangeable. This means\u00a0that when\u00a0<\/span>they have been\u00a0<\/span>defined,\u00a0they can not\u00a0<\/span>be modified. Many Python functions change strings,\u00a0like\u00a0<\/span>replace(), join(), and split(). They do not, however, alter the original string. They make\u00a0a duplicate<\/span>\u00a0of a string, alter it, then return it to the caller.<\/p>\n Given a string, the task is to scan the given two strings and print the larger string without using built-in functions in python.<\/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 is the full approach is to scan the given two strings and print the larger string without using built-in functions in python.<\/p>\n If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list<\/a> available.<\/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 Explanation:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Strings in Python: In Python, a string\u00a0may be a\u00a0sequence of characters. It is\u00a0an information\u00a0type that has been derived. Strings are unchangeable. This means\u00a0that when\u00a0they have been\u00a0defined,\u00a0they can not\u00a0be modified. Many Python functions change strings,\u00a0like\u00a0replace(), join(), and split(). They do not, however, alter the original string. They make\u00a0a duplicate\u00a0of a string, alter it, then return it …<\/p>\ngiven first string =btechgeeks\r\n\r\ngiven second string =python<\/pre>\n
The string btechgeeks is larger string<\/pre>\n
given first string =java\r\n\r\ngiven second string =javascript<\/pre>\n
The string javascript is larger string<\/pre>\n
Program to Take in Two Strings and Display the Larger String without Using Built-in Functions in Python<\/h2>\n
\n
1)Using Count Variable and conditional statements(Static Input)<\/h3>\n
\n
# Give the two strings as static input and store them in two separate variables.\r\nfirststrng = 'btechgeeks'\r\nsecondstrng = 'python'\r\n# Take a variable to say stringLength1 that stores the length of the given first string.\r\n# Initialize the stringLength1 to 0.\r\nstringLength1 = 0\r\n# Take a variable to say stringLength2 that stores the length of the given second string.\r\n# Initialize the stringLength2\u00a0to 0.\r\nstringLength2 = 0\r\n# Using for loop to traverse over the elements of the first string.\r\nfor charact in firststrng:\r\n # Increment the stringLength1 (Count Variable) by 1.\r\n stringLength1 = stringLength1+1\r\n# Using for loop to traverse over the elements of the second string.\r\nfor charact in secondstrng:\r\n # Increment the stringLength2 (Count Variable) by 1.\r\n stringLength2 = stringLength2+1\r\n# Compare the count variables(stringLength1 ,stringLength2) of both\r\n# the strings using if conditional statement.\r\n# If the stringlength1 is greater than stringlength2 then\r\n# print the first string using the print() function.\r\nif(stringLength1 > stringLength2):\r\n print('The string', firststrng, 'is larger string')\r\n# If the stringlength1 is equal to stringlength2 then\r\n# print both the strings are equal using the print() function.\r\nelif(stringLength1 == stringLength2):\r\n print('The strings', firststrng, 'and', secondstrng, 'are equal in size')\r\n# If the stringlength1 is less than stringlength2 then\r\n# print the second string using the print() function.\r\nelse:\r\n print('The string', secondstrng, 'is larger string')\r\n<\/pre>\n
The string btechgeeks is larger string<\/pre>\n
2)Using Count Variable and conditional statements(User Input)<\/h3>\n
\n
# Give the two strings as user input using the input() function\r\n# and store them in two separate variables.\r\nfirststrng = input('Enter some random first string = ')\r\nsecondstrng = input('Enter some random second string = ')\r\n# Take a variable to say stringLength1 that stores the length of the given first string.\r\n# Initialize the stringLength1 to 0.\r\nstringLength1 = 0\r\n# Take a variable to say stringLength2 that stores the length of the given second string.\r\n# Initialize the stringLength2\u00a0to 0.\r\nstringLength2 = 0\r\n# Using for loop to traverse over the elements of the first string.\r\nfor charact in firststrng:\r\n # Increment the stringLength1 (Count Variable) by 1.\r\n stringLength1 = stringLength1+1\r\n# Using for loop to traverse over the elements of the second string.\r\nfor charact in secondstrng:\r\n # Increment the stringLength2 (Count Variable) by 1.\r\n stringLength2 = stringLength2+1\r\n# Compare the count variables(stringLength1 ,stringLength2) of both\r\n# the strings using if conditional statement.\r\n# If the stringlength1 is greater than stringlength2 then\r\n# print the first string using the print() function.\r\nif(stringLength1 > stringLength2):\r\n print('The string', firststrng, 'is larger string')\r\n# If the stringlength1 is equal to stringlength2 then\r\n# print both the strings are equal using the print() function.\r\nelif(stringLength1 == stringLength2):\r\n print('The strings', firststrng, 'and', secondstrng, 'are equal in size')\r\n# If the stringlength1 is less than stringlength2 then\r\n# print the second string using the print() function.\r\nelse:\r\n print('The string', secondstrng, 'is larger string')\r\n<\/pre>\n
Enter some random first string = vikram\r\nEnter some random second string = vishalraj\r\nThe string vishalraj is larger string<\/pre>\n
\n
\n