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
What's difference between "depends on" and "is blocked by" relations between issues in Jira next-gen board?
Why was this character made Grand Maester?
Job Market: should one hide their (young) age?
How to let other coworkers know that I don't share my coworker's political views?
How was Daenerys able to legitimise this character?
USPS Back Room - Trespassing?
Why didn't Thanos use the Time Stone to stop the Avengers' plan?
Navigating a quick return to previous employer
Shorten or merge multiple lines of `&> /dev/null &`
Why are GND pads often only connected by four traces?
How do I disable login of user?
Testing using real data of the customer
How to politely tell someone they did not hit "reply to all" in an email?
Dad jokes are fun
Why did the person in charge of a principality not just declare themself king?
Is categoricity retained when reducing the language?
Replacement stem cap and bolt
How to deal with a colleague who is being aggressive?
What could a self-sustaining lunar colony slowly lose that would ultimately prove fatal?
What did the 'turbo' button actually do?
Can a person survive on blood in place of water?
Must a warlock replace spells with new spells of exactly their Pact Magic spell slot level?
How to keep consistency across the application architecture as a team grows?
Drums and punctuation
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.
python performance python-3.x strings
$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.
python performance python-3.x strings
$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.
python performance python-3.x strings
$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.
python performance python-3.x strings
python performance python-3.x strings
edited 16 mins ago
200_success
132k20160427
132k20160427
asked 9 hours ago
JustinJustin
371116
371116
add a comment |
add a comment |
3 Answers
3
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
vurmux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
What doeswakastand 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
8 hours ago
$begingroup$
Upvoted! Thanks, I will keep in mind to use inbuilt functions as they make programs easier to write.
$endgroup$
– Justin
8 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
5 hours ago
1
$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
1
$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
44 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
39 mins ago
$begingroup$
@RolandIllig While the term "ternary" refers to a function with three arguments, the term is often used to refer to a specific ternary operator (a description of which is linked to in the answer). This answer builds that functionality out of binary operators.
$endgroup$
– Acccumulation
8 mins ago
add a comment |
$begingroup$
Here's how I would get the longer string:
max(string_1, string_2, key=len) # Returns the longer string
The key keyword argument is a pattern you'll see frequently in python. It accepts a function as an argument (in our case len).
If you wanted to find the longest of multiple strings, you could do that too:
max('a', 'bc', 'def', 'ghi', 'jklm', key=len) # => 'jklm'
New contributor
user201327 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
Some of these concepts may be foreign to a beginner programmer, but when you're ready I highly recommend taking a look at this python how-to document: docs.python.org/3.7/howto/functional.htmlwhen the student is ready, the teacher will appear
$endgroup$
– user201327
16 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
3 Answers
3
active
oldest
votes
3 Answers
3
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
vurmux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
What doeswakastand 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
8 hours ago
$begingroup$
Upvoted! Thanks, I will keep in mind to use inbuilt functions as they make programs easier to write.
$endgroup$
– Justin
8 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
vurmux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
What doeswakastand 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
8 hours ago
$begingroup$
Upvoted! Thanks, I will keep in mind to use inbuilt functions as they make programs easier to write.
$endgroup$
– Justin
8 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
vurmux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$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
vurmux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 8 hours ago
New contributor
vurmux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 8 hours ago
vurmuxvurmux
2809
2809
New contributor
vurmux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
vurmux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$begingroup$
What doeswakastand 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
8 hours ago
$begingroup$
Upvoted! Thanks, I will keep in mind to use inbuilt functions as they make programs easier to write.
$endgroup$
– Justin
8 hours ago
add a comment |
$begingroup$
What doeswakastand 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
8 hours ago
$begingroup$
Upvoted! Thanks, I will keep in mind to use inbuilt functions as they make programs easier to write.
$endgroup$
– Justin
8 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
8 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
8 hours ago
$begingroup$
Upvoted! Thanks, I will keep in mind to use inbuilt functions as they make programs easier to write.
$endgroup$
– Justin
8 hours ago
$begingroup$
Upvoted! Thanks, I will keep in mind to use inbuilt functions as they make programs easier to write.
$endgroup$
– Justin
8 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
5 hours ago
1
$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
1
$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
44 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
39 mins ago
$begingroup$
@RolandIllig While the term "ternary" refers to a function with three arguments, the term is often used to refer to a specific ternary operator (a description of which is linked to in the answer). This answer builds that functionality out of binary operators.
$endgroup$
– Acccumulation
8 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
5 hours ago
1
$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
1
$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
44 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
39 mins ago
$begingroup$
@RolandIllig While the term "ternary" refers to a function with three arguments, the term is often used to refer to a specific ternary operator (a description of which is linked to in the answer). This answer builds that functionality out of binary operators.
$endgroup$
– Acccumulation
8 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 5 hours ago
answered 5 hours ago
WeRelicWeRelic
1944
1944
1
$begingroup$
Upvoted! Thanks for the detailed response! Some really good stuff in here!
$endgroup$
– Justin
5 hours ago
1
$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
1
$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
44 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
39 mins ago
$begingroup$
@RolandIllig While the term "ternary" refers to a function with three arguments, the term is often used to refer to a specific ternary operator (a description of which is linked to in the answer). This answer builds that functionality out of binary operators.
$endgroup$
– Acccumulation
8 mins ago
add a comment |
1
$begingroup$
Upvoted! Thanks for the detailed response! Some really good stuff in here!
$endgroup$
– Justin
5 hours ago
1
$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
1
$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
44 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
39 mins ago
$begingroup$
@RolandIllig While the term "ternary" refers to a function with three arguments, the term is often used to refer to a specific ternary operator (a description of which is linked to in the answer). This answer builds that functionality out of binary operators.
$endgroup$
– Acccumulation
8 mins ago
1
1
$begingroup$
Upvoted! Thanks for the detailed response! Some really good stuff in here!
$endgroup$
– Justin
5 hours ago
$begingroup$
Upvoted! Thanks for the detailed response! Some really good stuff in here!
$endgroup$
– Justin
5 hours ago
1
1
$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
1
1
$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
44 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
44 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
39 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
39 mins ago
$begingroup$
@RolandIllig While the term "ternary" refers to a function with three arguments, the term is often used to refer to a specific ternary operator (a description of which is linked to in the answer). This answer builds that functionality out of binary operators.
$endgroup$
– Acccumulation
8 mins ago
$begingroup$
@RolandIllig While the term "ternary" refers to a function with three arguments, the term is often used to refer to a specific ternary operator (a description of which is linked to in the answer). This answer builds that functionality out of binary operators.
$endgroup$
– Acccumulation
8 mins ago
add a comment |
$begingroup$
Here's how I would get the longer string:
max(string_1, string_2, key=len) # Returns the longer string
The key keyword argument is a pattern you'll see frequently in python. It accepts a function as an argument (in our case len).
If you wanted to find the longest of multiple strings, you could do that too:
max('a', 'bc', 'def', 'ghi', 'jklm', key=len) # => 'jklm'
New contributor
user201327 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
Some of these concepts may be foreign to a beginner programmer, but when you're ready I highly recommend taking a look at this python how-to document: docs.python.org/3.7/howto/functional.htmlwhen the student is ready, the teacher will appear
$endgroup$
– user201327
16 mins ago
add a comment |
$begingroup$
Here's how I would get the longer string:
max(string_1, string_2, key=len) # Returns the longer string
The key keyword argument is a pattern you'll see frequently in python. It accepts a function as an argument (in our case len).
If you wanted to find the longest of multiple strings, you could do that too:
max('a', 'bc', 'def', 'ghi', 'jklm', key=len) # => 'jklm'
New contributor
user201327 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
Some of these concepts may be foreign to a beginner programmer, but when you're ready I highly recommend taking a look at this python how-to document: docs.python.org/3.7/howto/functional.htmlwhen the student is ready, the teacher will appear
$endgroup$
– user201327
16 mins ago
add a comment |
$begingroup$
Here's how I would get the longer string:
max(string_1, string_2, key=len) # Returns the longer string
The key keyword argument is a pattern you'll see frequently in python. It accepts a function as an argument (in our case len).
If you wanted to find the longest of multiple strings, you could do that too:
max('a', 'bc', 'def', 'ghi', 'jklm', key=len) # => 'jklm'
New contributor
user201327 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
Here's how I would get the longer string:
max(string_1, string_2, key=len) # Returns the longer string
The key keyword argument is a pattern you'll see frequently in python. It accepts a function as an argument (in our case len).
If you wanted to find the longest of multiple strings, you could do that too:
max('a', 'bc', 'def', 'ghi', 'jklm', key=len) # => 'jklm'
New contributor
user201327 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user201327 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 19 mins ago
user201327user201327
111
111
New contributor
user201327 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user201327 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$begingroup$
Some of these concepts may be foreign to a beginner programmer, but when you're ready I highly recommend taking a look at this python how-to document: docs.python.org/3.7/howto/functional.htmlwhen the student is ready, the teacher will appear
$endgroup$
– user201327
16 mins ago
add a comment |
$begingroup$
Some of these concepts may be foreign to a beginner programmer, but when you're ready I highly recommend taking a look at this python how-to document: docs.python.org/3.7/howto/functional.htmlwhen the student is ready, the teacher will appear
$endgroup$
– user201327
16 mins ago
$begingroup$
Some of these concepts may be foreign to a beginner programmer, but when you're ready I highly recommend taking a look at this python how-to document: docs.python.org/3.7/howto/functional.html
when the student is ready, the teacher will appear$endgroup$
– user201327
16 mins ago
$begingroup$
Some of these concepts may be foreign to a beginner programmer, but when you're ready I highly recommend taking a look at this python how-to document: docs.python.org/3.7/howto/functional.html
when the student is ready, the teacher will appear$endgroup$
– user201327
16 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