Python string slice step? [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to convert python codes to java. But I do not understand slicing step parameter.
Example:
x = "Hello World !"
x[6:2:-1]
And what is the result x[6:2:-1] ?

-1 step just reverts the direction of the slice:
>>> x = "Hello World !"
>>> x[6]
'W'
>>> x[2]
'l'
>>> x[6:2:-1]
'W ol'
[6:2:-1] means give me a slice from the 6th to the 2nd item (not including), reversed.
FYI, you don't need python installed for checking the result of the code you've asked about, go to pythonanywhere and play:
PythonAnywhere is a Python development and hosting environment that
displays in your web browser and runs on our servers.
Also see:
Explain Python's slice notation
Extended Slices

Related

Reverse and split string: Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to reverse and split a string in Python. Please suggest how?
'this is Xing Min' should return ['niM', 'gniX', 'si', 'siht'].
You can do it as:
my_str[::-1].split()
Example
>>> s = 'Hello World'
>>> print s[::-1].split()
['dlroW'. 'olleH']
>>> s = 'this is Xing Min'
>>> print s[::-1].split()
['niM', 'gniX', 'si', 'siht']
Here, the [::-1] gets the whole string in reverse order. This is the syntax [start:end:step]. When you don't specify a start and end, it will deal with the whole string. When you do [::-1], the step value is -1 which means that the string is read in reverse.

Please help me understand this - Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
Can someone please explain how this works? Like, how does it count?
for a in range(2,5):
for b in range(1,2):
print (a+b,end=" ")
print("---",end=" ")
the output is: 3 --- 4 --- 5 ---
It just adds 2, 3, 4 with 1 and prints the result.
In Python-3.x, print() is a function and the argument end in it means
string appended after the last value, default a newline
Values returned by range(x, y) are [x, y-1]
If you don't know the usage of a function, you could open ipython and type something like the following:
help(print)
help(range)

Check if part of text is in tuple [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to check if part of text is in tuple?
For example:
my_data = ((1234L,), (23456L,), (3333L,))
And we need to find if 123 or 1234 is in tuple.
I didn't worked lot with tuples before.
In array we use:
if variable in array
But its not working for tuples like my_data
PS. 1st answer solved problem.
def findIt(data, num):
num = str(num)
return any(num in str(i) for item in data for i in item)
data = ((1234L,), (23456L,), (3333L,))
print findIt(data, 123)
Output
True

How to split array of strings into sub-arrays according to special starting character? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can you simplify this code from Python into Ruby? Lets say I have this data
data = ['hello', 'person', ';hello', 'otherperson']
print([x.split("#") for x in "#".join(data).split(";")])
When I print it it prints this:
[['hello', 'person', ''], ['hello', 'otherperson']]
Is there something like this in Ruby? If it can be accomplished in one line, I would prefer that, but I'm after just knowing how it is.
Literally translated,
data.join(?#).split(?;).map { |x| x.split(?#) }
But you might want a different approach entirely. This will misbehave if any of the strings contain #.
This works for intended output, but do note it modifies original strings, so ideally data is a deep clone (or otherwise not a problem to alter contained strings):
data.slice_before { |s| s.gsub!(/^;/,'') }.to_a
=> [["hello", "person"], ["hello", "otherperson"]]

Make print come after `if` in python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I'm making a quiz and when I make an if statement in python to put a question I have to put the print before it(which I'm using at answers). So my question is how to make it come after without stopping the if statement. Any ideas appreciated. Sorry if its hard to know what I'm trying to say I'm a little new to python.
Something like this maybe?
answer = input ('Who was the last king of Rome? ') #use raw_input for python2.x
if answer == 'Tarquinius': print ('correct')
else: print ('incorrect')
print 'asdf' if 1 == 2 else 'fdsa'
Or if you don't get that this does, try this:
print "ok..nice" if raw_input("how are you ") == "ok" else "well, that's too bad"
Apparently people don't know the new inline if syntax, and think i'm trolling.
This should all be written in 1 line and does not require splitting.

Categories

Resources