The for loop in python is structured in the following way:
for iteration_variable in iteratable_object:
# do task with iteration_variable
# repeat with next iteration variable
here is an example of printing out numbers:
for i in range(1,10):
print i
And will output
1
2
3
.
.
.
9
Abbreviated to save typing.
For strings (str is the type ascribed to regular strings) this can be done via a list:
my_string_array = ['abc','def','ghi']
for my_string in my_string_array:
print my_string
And will output
abc
def
ghi
Now to discuss a few of the comparable commands to the javascript commands you used. To handle user input python has a few options. The best for simple string input is raw_input, and is used in the following way:
variable = raw_input("Hello, who are you?")
This will print out Hello, who are you?, the user can then enter any text they like and hit the enter key and it will be stored in variable now for your use.
If you want to replace a character in a string, python's str type has a method called replace, which works in the following way:
my_variable = 'abc.def'
my_variable = my_variable.replace('.','')
print my_variable
will output
abcdef
the method str.replace will return the string with all character strings matching the first argument replaced with the second argument in the same way the javascript function works. The method only returns the new string though, it does not automatically change the value of the variable, so if you desire to keep it save it into a variable (saving it into itself is a valid option as seen in my example).
To make a string lowercase you can use str's the built in command lower. In a similar way to replace, the updated string is returned and not saved into the variable and should be stored somewhere again if further use is required.
my_var = 'ABC_DEF'
my_var = my_var.lower()
print my_var
and will output
abc_def
Putting this all together we can convert your javascript loop into python in the following way:
name = raw_inpout("enter a name");
for i in range(len(name)): # len is the python method to
# get the length of an object
name = name.replace(".", "").replace("'", "").replace("-", "")
name = name.lower()
print name
This is a direct translation of your code. In python you can iterate directly over characters in a string and that is usually preferred:
my_string = 'abc'
for character in my_string:
print character
will output
a
b
c
It might be helpful to know that in your case you do not actually need the for loop in either the javascript or the python case:
# Python
name = raw_inpout("enter a name");
name = name.replace(".", "").replace("'", "").replace("-", "")
name = name.lower()
print name
And
# Javascript
name = prompt("enter a name");
name = name.replace(".", "").replace("'", "").replace("-", "");
name = name.toLowerCase();
alert(name);
This is because both the javascript and python methods apply to the whole string already and you do not need to make changes character by character. Indeed you do not use the index reference to which you are looping over anyway! You just end up making the same change to the string as many times as the string is long.
Hopefully this gives you a better picture of for loops and a brief look at python! Happy python-ing!
replacecan be done outside of the loop as well as thelowerin python and in javascript. Why are you using a loop?