trying to advance from java to Python with no luck [duplicate] - python

This question already has answers here:
Iterating each character in a string using Python
(9 answers)
Closed 9 years ago.
so i want to write this simple java code in python, but i cant, i want to print all the Strings letters form the first index to the last, so if my string is "josh", i want to print:
j
o
s
h
or like this java code:
String name = josh;
for(int i = 0; i < josh.length(); i++){
print(josh.CharAt(i));
}
i cant find any method that is like charAt, i'm guessing that it doesn't exists
but there has to some other way to do it, i know its kinda dumb question, but i couldn't
find anything online or here(on this website) to help me, so any ideas?

Try this:
name = "josh"
for i in name:
print i
# print(i) # python 3
name is the variable to which we assign string literal "josh". name is a str (or unicode in Python 3)
we iterate over name since strings are iterable (have __iter__() methods) with the loop iteration syntax. Each consecutive character is assigned to loop variable i per iteration over name's length.
Note that we cannot assign to name[i], only read from it since strings are immutable in python.

Related

Naming a list with a number [duplicate]

This question already has answers here:
Can variable names in Python start with an integer?
(2 answers)
Closed 1 year ago.
Can you name a list with the first character being a number? I get an error when I try to do this. I am taking names of products and making lists and dictionaries with them and some of the names of the products start with a number.
like:
11T25M = ["Grade", "Tensile", "Elongation"]
I get
11T25M = ["Grade", "Tensile", "Elongation"]
^
Syntax Error: invalid decimal literal
No, you're not allowed to start a variable name with a number, regardless of the type of the variable (i.e. not just lists), the parser disallows it. This is fairly common amongst a lot of languages.
2e1 is a valid number in python (20), imagine if you wrote my_var = 2e1, is 2e1 a variable or a number? How is the parser to know?
Technically you could force a variable starting with a number for the name to exist through globals() but other than being terrible practice, it would be such a faff it's not worth the time or extra writing it would require.
No you cannot start a variable name with a number. One solution is instead of having a bunch of variables, you can contain them all in a dictionary.
my_dict = {}
my_dict["11T25M"] = ["Grade", "Tensile", "Elongation"]
print(my_dict["11T25M"])
Now instead of having a list called 11T25M, you have a list called my_dict["11T25M"]

Usage of colon and equals signs in Python [duplicate]

This question already has answers here:
What are variable annotations?
(2 answers)
Closed 2 years ago.
class PrepareTableOperator(BaseOperator):
def _load_table(self):
drop_table_query: str = ( "drop table if exists " + self.get_table() )
I'm a complete newbie to python but I do have a bit of a Java background.
What I don't get is the usage of colons in python. I've googled around, and it's used for slicing and for starting function definitions. But there's no 'def' syntax in the above, so to me this doesn't look like a function.
My question is, what is the colon in Python, is it another assignment operator for dictionary values, similar to a key value pair? Is that what it's doing here? What is it doing here, essentially?
In your code snippet, it is a type annotation. It is a relatively new feature of Python that lets you keep track of the data types, so in this case, it is declaring that drop_table_query is a string.
Type annotations are checked by IDE, but not enforced by the Python interpreter. This means that drop_table_query could actually be an int and Python itself won't complain. The type annotation is just a sort of recommendation.
What it is doing here is defining that the variable will be a string (str data type). The colon is used in while loops, for loops, if statements, and functions. The colon helps the code flow into the following indented block. A single equal sign is used to assign a value to a variable, a double equal sign is used for conditions, like if var == other_var:. There is also +=, -=, *=, and /=. Those are used to shorten things like, var = var + 1, to var += 1.

How to delete the last character in a string in python 3 [duplicate]

This question already has answers here:
Remove final character from string
(5 answers)
Closed 4 years ago.
I'm trying to delete the last character of a string, and every documentation I can find says that this works.
string = 'test'
string[:-1]
print(string)
However, whenever I try it, my IDE tells me that line two has no effect, and when I run the code it outputs "test" and not "tes", which is what I want it to do. I think that the documentation I'm reading is about python 2 and not 3, because I don't understand why else this simple code wouldn't work. Can someone show me how to remove the last letter of a string in python 3?
new_string = string[:-1]
print(new_string)
You must save the string in the memory. When we assign a variable to the string without the last character, the variable then "stores" the new value. Thus we can print it out.

How to find the position of every instance of a character in a list [duplicate]

This question already has answers here:
How do i find the position of MORE THAN ONE substring in a string (Python 3.4.3 shell)
(3 answers)
Closed 6 years ago.
I have a program where I need to identify the location of every instance of the letter A in a quote. Something like I would do with quote.index("A"), but I need every instance of A, not just the first.
I know this question has been asked before but I'm very, very new to Python and I'm having trouble understanding the answers to those questions.
If anyone could give me a dumbed down explanation of how to do this, I'd be incredibly thankful because I'm utterly lost.
If i understand correctly, you have a string and you want to keep all A's locations in e different array.
Then you can try something like that.
quote = "some quote"
locs = []
for i in range(len(quote)) :
if quote[i] == 'A' :
locs.append(i)
print(locs)

Count letters in string python [duplicate]

This question already has answers here:
How to get the size of a string in Python?
(6 answers)
Closed 7 years ago.
(New to python and stack overflow)
I was curious if there was a way to count the amount of letters in a string for python. for example:
string="hello"
I just want something to count the letters then output it into a variable for later use.
The following will give the length of a string:
len(string)
In your case, you can assign it:
numLetters = len(string)
This function can be used for other objects besides strings. For additional uses, read the documentation.
Use python function len, i.e.:
size = len(string)
len()
https://docs.python.org/2/library/functions.html#len
DEMO
https://ideone.com/mhpdLi

Categories