Python program to take in two strings and print the larger stringPython: Combing two programs that analyze stringsTwo and a sub stringGiven two strings, a and b, determine the minimum number of character deletions required to make a and b anagramsAsk the user for two numbers, then add or multiply themProgram for finding longest run of zeros from a list of 100 random integers which are either 0 or 1Program for CodeHS 8.3.8: Word Ladder in Python 3Python program for fibonacci sequence using a recursive functionPython program to find the subarray with maximum sumGet all possible subsets from a set of distinct integers using OOPPython program to find the factorial of a number using recursion
Is my plasma cannon concept viable?
Can I tell a prospective employee that everyone in the team is leaving?
Co-author wants to put their current funding source in the acknowledgements section because they edited the paper
Beginner looking to learn/master musical theory and instrumental ability. Where should I begin?
Which European Languages are not Indo-European?
How do I superimpose two math symbols?
Need to read my home electrical Meter
Did this character show any indication of wanting to rule before S8E6?
How to make the Bass in SATB move more smoothly?
Why do Russians almost not use verbs of possession akin to "have"?
Why isn't 'chemically-strengthened glass' made with potassium carbonate to begin with?
Why did it take so long for Germany to allow electric scooters / e-rollers on the roads?
Why does the hash of infinity have the digits of π?
Are runways booked by airlines to land their planes?
Natural Armour and Weapons
Writing style before Elements of Style
Grade-school elementary algebra presented in an abstract-algebra style?
How to melt snow without fire or body heat?
Time complexity of an algorithm: Is it important to state the base of the logarithm?
The disk image is 497GB smaller than the target device
How to patch glass cuts in a bicycle tire?
Did 20% of US soldiers in Vietnam use heroin, 95% of whom quit afterwards?
Why haven't we yet tried accelerating a space station with people inside to a near light speed?
Is keeping the forking link on a true fork necessary (Github/GPL)?
Python program to take in two strings and print the larger string
Python: Combing two programs that analyze stringsTwo and a sub stringGiven two strings, a and b, determine the minimum number of character deletions required to make a and b anagramsAsk the user for two numbers, then add or multiply themProgram for finding longest run of zeros from a list of 100 random integers which are either 0 or 1Program for CodeHS 8.3.8: Word Ladder in Python 3Python program for fibonacci sequence using a recursive functionPython program to find the subarray with maximum sumGet all possible subsets from a set of distinct integers using OOPPython program to find the factorial of a number using recursion
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I have written a Python program to take in two strings and print the larger of the two strings.
Here is my code -
string1 = input("Enter first string: ")
string2 = input("Enter second string: ")
count1 = 0
count2 = 0
for i in string1:
count1 = count1 + 1
for j in string2:
count2 = count2 + 1
if (count1 < count2):
print ("Larger string is:")
print (string2)
elif (count1 == count2):
print ("Both strings are equal.")
else:
print ("Larger string is:")
print (string1)
Here are some example outputs -
Enter first string: everything
Enter second string: nothing
Larger string is:
everything
Enter first string: cat
Enter second string: apple
Larger string is:
apple
So, I would like to know whether I could make this program shorter and more efficient.
Any help would be highly appreciated.
python performance python-3.x
$endgroup$
add a comment |
$begingroup$
I have written a Python program to take in two strings and print the larger of the two strings.
Here is my code -
string1 = input("Enter first string: ")
string2 = input("Enter second string: ")
count1 = 0
count2 = 0
for i in string1:
count1 = count1 + 1
for j in string2:
count2 = count2 + 1
if (count1 < count2):
print ("Larger string is:")
print (string2)
elif (count1 == count2):
print ("Both strings are equal.")
else:
print ("Larger string is:")
print (string1)
Here are some example outputs -
Enter first string: everything
Enter second string: nothing
Larger string is:
everything
Enter first string: cat
Enter second string: apple
Larger string is:
apple
So, I would like to know whether I could make this program shorter and more efficient.
Any help would be highly appreciated.
python performance python-3.x
$endgroup$
add a comment |
$begingroup$
I have written a Python program to take in two strings and print the larger of the two strings.
Here is my code -
string1 = input("Enter first string: ")
string2 = input("Enter second string: ")
count1 = 0
count2 = 0
for i in string1:
count1 = count1 + 1
for j in string2:
count2 = count2 + 1
if (count1 < count2):
print ("Larger string is:")
print (string2)
elif (count1 == count2):
print ("Both strings are equal.")
else:
print ("Larger string is:")
print (string1)
Here are some example outputs -
Enter first string: everything
Enter second string: nothing
Larger string is:
everything
Enter first string: cat
Enter second string: apple
Larger string is:
apple
So, I would like to know whether I could make this program shorter and more efficient.
Any help would be highly appreciated.
python performance python-3.x
$endgroup$
I have written a Python program to take in two strings and print the larger of the two strings.
Here is my code -
string1 = input("Enter first string: ")
string2 = input("Enter second string: ")
count1 = 0
count2 = 0
for i in string1:
count1 = count1 + 1
for j in string2:
count2 = count2 + 1
if (count1 < count2):
print ("Larger string is:")
print (string2)
elif (count1 == count2):
print ("Both strings are equal.")
else:
print ("Larger string is:")
print (string1)
Here are some example outputs -
Enter first string: everything
Enter second string: nothing
Larger string is:
everything
Enter first string: cat
Enter second string: apple
Larger string is:
apple
So, I would like to know whether I could make this program shorter and more efficient.
Any help would be highly appreciated.
python performance python-3.x
python performance python-3.x
edited 8 hours ago
Justin
asked 8 hours ago
JustinJustin
371116
371116
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Python strings supports Python built-in len function. You don't need to iterate through them manually, as for lists/dicts/sets etc (it is not Pythonic):
def compare_strings_len(s1, s2):
if len(s1) > len(s2):
print('String 1 is longer: ', s1)
elif len(s1) < len(s2):
print('String 2 is longer: ', s2)
else:
print('Strings length are equal!')
New contributor
$endgroup$
$begingroup$
What doeswaka
stand for? I.e since you already made the good suggestion to put this code into a function, at least give it a helpful name.
$endgroup$
– Graipher
7 hours ago
$begingroup$
Upvoted! Thanks, I will keep in mind to use inbuilt functions as they make programs easier to write.
$endgroup$
– Justin
7 hours ago
add a comment |
$begingroup$
Long live the Ternary:
def print_longer(s,s2):
# return (s,s2)[len(s)<len(s2)] if you don't want to print within the function.
print( ( s, s2 )[ len(s) < len(s2) ] )
Explanation:
if-else statements are clean, but they're verbose. A ternary operation would reduce this to a one-liner.
The format is as follows: (result_if_false,result_if_true)[comparison]
What is happening is that (s,s2)
is creating a tuple of the two strings. len(s)<len(s2)
then compares the two, and because they're within square brackets []
; the boolean result is casted to an integer index.
Since you can only have a 0 or 1 result, this returns s
if it is larger than s2
, and vice-versa.
EDIT: This returns s
if both strings are of equal lengths.
$endgroup$
1
$begingroup$
Upvoted! Thanks for the detailed response! Some really good stuff in here!
$endgroup$
– Justin
4 hours ago
$begingroup$
It doesn't properly handle the case of"Both strings are equal."
but upvoted anyways because it's a nice approach
$endgroup$
– Andres
3 hours ago
$begingroup$
The expression in your answer is not a ternary operator. By definition, a ternary operator takes 3 arguments. Your code uses binary operators instead.
$endgroup$
– Roland Illig
16 mins ago
$begingroup$
It is interesting and really short gimmick, but I definetly don't use this in production code :) I spent nearly 10-20 seconds looking at the code until I understood how it is working!
$endgroup$
– vurmux
12 mins ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f220726%2fpython-program-to-take-in-two-strings-and-print-the-larger-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Python strings supports Python built-in len function. You don't need to iterate through them manually, as for lists/dicts/sets etc (it is not Pythonic):
def compare_strings_len(s1, s2):
if len(s1) > len(s2):
print('String 1 is longer: ', s1)
elif len(s1) < len(s2):
print('String 2 is longer: ', s2)
else:
print('Strings length are equal!')
New contributor
$endgroup$
$begingroup$
What doeswaka
stand for? I.e since you already made the good suggestion to put this code into a function, at least give it a helpful name.
$endgroup$
– Graipher
7 hours ago
$begingroup$
Upvoted! Thanks, I will keep in mind to use inbuilt functions as they make programs easier to write.
$endgroup$
– Justin
7 hours ago
add a comment |
$begingroup$
Python strings supports Python built-in len function. You don't need to iterate through them manually, as for lists/dicts/sets etc (it is not Pythonic):
def compare_strings_len(s1, s2):
if len(s1) > len(s2):
print('String 1 is longer: ', s1)
elif len(s1) < len(s2):
print('String 2 is longer: ', s2)
else:
print('Strings length are equal!')
New contributor
$endgroup$
$begingroup$
What doeswaka
stand for? I.e since you already made the good suggestion to put this code into a function, at least give it a helpful name.
$endgroup$
– Graipher
7 hours ago
$begingroup$
Upvoted! Thanks, I will keep in mind to use inbuilt functions as they make programs easier to write.
$endgroup$
– Justin
7 hours ago
add a comment |
$begingroup$
Python strings supports Python built-in len function. You don't need to iterate through them manually, as for lists/dicts/sets etc (it is not Pythonic):
def compare_strings_len(s1, s2):
if len(s1) > len(s2):
print('String 1 is longer: ', s1)
elif len(s1) < len(s2):
print('String 2 is longer: ', s2)
else:
print('Strings length are equal!')
New contributor
$endgroup$
Python strings supports Python built-in len function. You don't need to iterate through them manually, as for lists/dicts/sets etc (it is not Pythonic):
def compare_strings_len(s1, s2):
if len(s1) > len(s2):
print('String 1 is longer: ', s1)
elif len(s1) < len(s2):
print('String 2 is longer: ', s2)
else:
print('Strings length are equal!')
New contributor
edited 7 hours ago
New contributor
answered 8 hours ago
vurmuxvurmux
2809
2809
New contributor
New contributor
$begingroup$
What doeswaka
stand for? I.e since you already made the good suggestion to put this code into a function, at least give it a helpful name.
$endgroup$
– Graipher
7 hours ago
$begingroup$
Upvoted! Thanks, I will keep in mind to use inbuilt functions as they make programs easier to write.
$endgroup$
– Justin
7 hours ago
add a comment |
$begingroup$
What doeswaka
stand for? I.e since you already made the good suggestion to put this code into a function, at least give it a helpful name.
$endgroup$
– Graipher
7 hours ago
$begingroup$
Upvoted! Thanks, I will keep in mind to use inbuilt functions as they make programs easier to write.
$endgroup$
– Justin
7 hours ago
$begingroup$
What does
waka
stand for? I.e since you already made the good suggestion to put this code into a function, at least give it a helpful name.$endgroup$
– Graipher
7 hours ago
$begingroup$
What does
waka
stand for? I.e since you already made the good suggestion to put this code into a function, at least give it a helpful name.$endgroup$
– Graipher
7 hours ago
$begingroup$
Upvoted! Thanks, I will keep in mind to use inbuilt functions as they make programs easier to write.
$endgroup$
– Justin
7 hours ago
$begingroup$
Upvoted! Thanks, I will keep in mind to use inbuilt functions as they make programs easier to write.
$endgroup$
– Justin
7 hours ago
add a comment |
$begingroup$
Long live the Ternary:
def print_longer(s,s2):
# return (s,s2)[len(s)<len(s2)] if you don't want to print within the function.
print( ( s, s2 )[ len(s) < len(s2) ] )
Explanation:
if-else statements are clean, but they're verbose. A ternary operation would reduce this to a one-liner.
The format is as follows: (result_if_false,result_if_true)[comparison]
What is happening is that (s,s2)
is creating a tuple of the two strings. len(s)<len(s2)
then compares the two, and because they're within square brackets []
; the boolean result is casted to an integer index.
Since you can only have a 0 or 1 result, this returns s
if it is larger than s2
, and vice-versa.
EDIT: This returns s
if both strings are of equal lengths.
$endgroup$
1
$begingroup$
Upvoted! Thanks for the detailed response! Some really good stuff in here!
$endgroup$
– Justin
4 hours ago
$begingroup$
It doesn't properly handle the case of"Both strings are equal."
but upvoted anyways because it's a nice approach
$endgroup$
– Andres
3 hours ago
$begingroup$
The expression in your answer is not a ternary operator. By definition, a ternary operator takes 3 arguments. Your code uses binary operators instead.
$endgroup$
– Roland Illig
16 mins ago
$begingroup$
It is interesting and really short gimmick, but I definetly don't use this in production code :) I spent nearly 10-20 seconds looking at the code until I understood how it is working!
$endgroup$
– vurmux
12 mins ago
add a comment |
$begingroup$
Long live the Ternary:
def print_longer(s,s2):
# return (s,s2)[len(s)<len(s2)] if you don't want to print within the function.
print( ( s, s2 )[ len(s) < len(s2) ] )
Explanation:
if-else statements are clean, but they're verbose. A ternary operation would reduce this to a one-liner.
The format is as follows: (result_if_false,result_if_true)[comparison]
What is happening is that (s,s2)
is creating a tuple of the two strings. len(s)<len(s2)
then compares the two, and because they're within square brackets []
; the boolean result is casted to an integer index.
Since you can only have a 0 or 1 result, this returns s
if it is larger than s2
, and vice-versa.
EDIT: This returns s
if both strings are of equal lengths.
$endgroup$
1
$begingroup$
Upvoted! Thanks for the detailed response! Some really good stuff in here!
$endgroup$
– Justin
4 hours ago
$begingroup$
It doesn't properly handle the case of"Both strings are equal."
but upvoted anyways because it's a nice approach
$endgroup$
– Andres
3 hours ago
$begingroup$
The expression in your answer is not a ternary operator. By definition, a ternary operator takes 3 arguments. Your code uses binary operators instead.
$endgroup$
– Roland Illig
16 mins ago
$begingroup$
It is interesting and really short gimmick, but I definetly don't use this in production code :) I spent nearly 10-20 seconds looking at the code until I understood how it is working!
$endgroup$
– vurmux
12 mins ago
add a comment |
$begingroup$
Long live the Ternary:
def print_longer(s,s2):
# return (s,s2)[len(s)<len(s2)] if you don't want to print within the function.
print( ( s, s2 )[ len(s) < len(s2) ] )
Explanation:
if-else statements are clean, but they're verbose. A ternary operation would reduce this to a one-liner.
The format is as follows: (result_if_false,result_if_true)[comparison]
What is happening is that (s,s2)
is creating a tuple of the two strings. len(s)<len(s2)
then compares the two, and because they're within square brackets []
; the boolean result is casted to an integer index.
Since you can only have a 0 or 1 result, this returns s
if it is larger than s2
, and vice-versa.
EDIT: This returns s
if both strings are of equal lengths.
$endgroup$
Long live the Ternary:
def print_longer(s,s2):
# return (s,s2)[len(s)<len(s2)] if you don't want to print within the function.
print( ( s, s2 )[ len(s) < len(s2) ] )
Explanation:
if-else statements are clean, but they're verbose. A ternary operation would reduce this to a one-liner.
The format is as follows: (result_if_false,result_if_true)[comparison]
What is happening is that (s,s2)
is creating a tuple of the two strings. len(s)<len(s2)
then compares the two, and because they're within square brackets []
; the boolean result is casted to an integer index.
Since you can only have a 0 or 1 result, this returns s
if it is larger than s2
, and vice-versa.
EDIT: This returns s
if both strings are of equal lengths.
edited 4 hours ago
answered 4 hours ago
WeRelicWeRelic
1944
1944
1
$begingroup$
Upvoted! Thanks for the detailed response! Some really good stuff in here!
$endgroup$
– Justin
4 hours ago
$begingroup$
It doesn't properly handle the case of"Both strings are equal."
but upvoted anyways because it's a nice approach
$endgroup$
– Andres
3 hours ago
$begingroup$
The expression in your answer is not a ternary operator. By definition, a ternary operator takes 3 arguments. Your code uses binary operators instead.
$endgroup$
– Roland Illig
16 mins ago
$begingroup$
It is interesting and really short gimmick, but I definetly don't use this in production code :) I spent nearly 10-20 seconds looking at the code until I understood how it is working!
$endgroup$
– vurmux
12 mins ago
add a comment |
1
$begingroup$
Upvoted! Thanks for the detailed response! Some really good stuff in here!
$endgroup$
– Justin
4 hours ago
$begingroup$
It doesn't properly handle the case of"Both strings are equal."
but upvoted anyways because it's a nice approach
$endgroup$
– Andres
3 hours ago
$begingroup$
The expression in your answer is not a ternary operator. By definition, a ternary operator takes 3 arguments. Your code uses binary operators instead.
$endgroup$
– Roland Illig
16 mins ago
$begingroup$
It is interesting and really short gimmick, but I definetly don't use this in production code :) I spent nearly 10-20 seconds looking at the code until I understood how it is working!
$endgroup$
– vurmux
12 mins ago
1
1
$begingroup$
Upvoted! Thanks for the detailed response! Some really good stuff in here!
$endgroup$
– Justin
4 hours ago
$begingroup$
Upvoted! Thanks for the detailed response! Some really good stuff in here!
$endgroup$
– Justin
4 hours ago
$begingroup$
It doesn't properly handle the case of
"Both strings are equal."
but upvoted anyways because it's a nice approach$endgroup$
– Andres
3 hours ago
$begingroup$
It doesn't properly handle the case of
"Both strings are equal."
but upvoted anyways because it's a nice approach$endgroup$
– Andres
3 hours ago
$begingroup$
The expression in your answer is not a ternary operator. By definition, a ternary operator takes 3 arguments. Your code uses binary operators instead.
$endgroup$
– Roland Illig
16 mins ago
$begingroup$
The expression in your answer is not a ternary operator. By definition, a ternary operator takes 3 arguments. Your code uses binary operators instead.
$endgroup$
– Roland Illig
16 mins ago
$begingroup$
It is interesting and really short gimmick, but I definetly don't use this in production code :) I spent nearly 10-20 seconds looking at the code until I understood how it is working!
$endgroup$
– vurmux
12 mins ago
$begingroup$
It is interesting and really short gimmick, but I definetly don't use this in production code :) I spent nearly 10-20 seconds looking at the code until I understood how it is working!
$endgroup$
– vurmux
12 mins ago
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f220726%2fpython-program-to-take-in-two-strings-and-print-the-larger-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown