[Tutor] split a string inside a list
Mark Lawrence
breamoreboy at yahoo.co.uk
Sat May 9 17:32:09 CEST 2015
On 09/05/2015 16:17, Alex Kleider wrote:
> On 2015-05-08 20:24, Kayla Hiltermann wrote:
>> hi,
>>
>> i am trying to make a pythagorean triples checker (a^2 + b^2 = c^2).
>> the user enters three sides to a triangle and my code determines if it
>> is a pythagorean triple (aka right triangle) or not. i have the entire
>> code pretty much done, except i want to account for variability in
>> user input, like using commas or just spaces. the user input is
>> initially a string, but is converted to a list once run through
>> .split() . I would like to split the user input by commas or spaces,
>> so:
>> 3 4 5
>> 3,4,5
>> 3, 4, 5
>> all become: [“3", “4", “5"].
>
> Several solutions have been suggested (of which the re.findall approach
> appeals to me the most) but one that hasn't and might be worth
> considering is use of str.maketrans() and s.translate().
> https://docs.python.org/2/library/string.html#string.maketrans
> https://docs.python.org/2/library/string.html#string.translate
>
From https://docs.python.org/3/whatsnew/3.1.html#other-language-changes
"The string.maketrans() function is deprecated and is replaced by new
static methods, bytes.maketrans() and bytearray.maketrans(). This change
solves the confusion around which types were supported by the string
module. Now, str, bytes, and bytearray each have their own maketrans and
translate methods with intermediate translation tables of the
appropriate type."
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Tutor
mailing list