[Tutor] split a string inside a list

Alex Kleider akleider at sonic.net
Sat May 9 17:17:17 CEST 2015


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



More information about the Tutor mailing list