Python Day 37 – loops!

Today I learned how to take out irregular separators from a string of numbers and return an integer value. First you need to define a variable separators, then use a for loop to go through the list of numbers and reassign the non numeric values to the separator variable.

number = “9,234:233:212 4321,432:212”

separators = “”

for char in number:
if not char.isnumeric():
separators = separators + char

Then there’s a more complicated code which prints the values which I don’t completely understand yet. I’m going to copy paste the code below and try to explain it (keep in mind this is before I’ve gotten to this portion of the Udemy course yet so I could be very wrong):

values = “”.join(char if char not in separators else ” ” for char in number).split()
print([int(val) for val in values])

Here we’re assigning a variable “values”. We’re taking all the non separator characters and joining it to the variable, else we join an empty space ” “. I’m not completely sure why we need to .join this function. Is it because we first need to assign a blank value to the “values” variable?

Does this mean one cannot assign an if statement to a variable?

The second line of code prints the values in “values” as an integer. I think this is what automatically adds the comma in between the numbers as it’s taking the numbers as a list of numbers. I’m not completely sure though.

I am still confused on:

I was assigned an exercise to only print upper case letters in a string, but when using isupper(variable) I kept getting the error: name ‘isupper’ is not defined.

I looked it up and stack exchange kept recommending to just directly read the documentation. The documentation I found states that:

isupper() : Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.

So does this mean isupper isn’t the right function to use here? I need to get the index numbers of each upper case, but this just returns a boolean for the entire string. Hmm I’ll have to look more into this later. Right now it’s Christmas so I should probably go back to spending time with the family 🙂

Happy holidays!

Leave a Reply

Discover more from Isaac Gazmararian

Subscribe now to keep reading and get access to the full archive.

Continue reading