I'd like to create a slice object from a string; right now the only way seems through a cumbersome hacky eval statement
class getslice:
def __getitem__(self, idx): return idx[0]
eval("getslice()[%s, ...
|
For lists the method list.index(x) returns the index in the list of the first item whose value is x. But if I want to look inside the items, and not just ... |
I know that .index() will return where a substring is located in python.
However, what I want is to find where a substring is located for the nth time, which would ... |
I have a string
string='texttexttextblahblah",".'
and what I want to do is cut of some of the rightmost characters by indexing and assign it to string so that string will be equal to ... |
I have a the string 'Hello', I need to find out what characters occupy which indexes.
Pseudo-code:
string = 'Hello'
a = string.index(0)
b = string.index(4)
print a , b
a would be 'H' and b ... |
I am newbie in python and facing some problem , my problem is that how to insert some fields in already existing string
for example:
... |
When we need to slice a string at a particular location, we need to know the index from where we want to.
For example, in the string:
>>> s = 'Your ID ...
|
|
I suspect this is a regular expression problem - and a very basic one, so apologies.
In Python, if I have a string like
xdtwkeltjwlkejt7wthwk89lk
how can I get the index of ... |
Let's say I have a string
string = '1234567890'
and I want a slice of that string defined by another string
slice = '5:8'
This is easy to do with
>>>string[5:8]
'678'
However the slice is ... |
How can I write a function that returns the index of a string without using the index method a.k.a string.index('some random character')
Thanks in advance,
Jay
|
I have two strings:
a='1234512345'
b='abcdefghik'
I would like to search string "a" for occurrences of '1' and then print the positions of "b" that correspond to that index, i.e.
'af'
I can use
import re
starts=[match.start() ...
|
I have two sets of data that I am reading via nested for loops in Python. I need to match lines of the two different text files using a common ... |
I've been trying to help my friend with his assignment and I can't even figure out where to start. I can get it to identify the substring inside of the ... |
I am trying to build a dynamic password recovery tool. You can specify a password, and an unknown character list which correspond to unknown password indexes. So, if you remember 90% ... |
I'm working on teaching myself basic programming.
One simple project is to find the index of recurrences of a substring within a string. So for example, in string "abcdefdef" and substring "def", ... |
I've been trying to slice two characters out of a string using a loop, but instead of grabbing two characters, it only grabs one.
I've tried:
input[i:i+1]
and
input[i:(i+1)]
but neither seems to work.
How do I ... |
When I try to print splited_data[1] I'm getting error message IndexError: list index out of range, On the other hand splited_data[0] is working fine.
I want to insert data into MySQL. splited_data[0] ... |
I am creating a game where the program will read from a file that contains 5 multiple choice questions and the user can answer this questions and receive a score. However ... |
I changed from optparse to argparse but when I try to run it I get the following error:
if not option_string[0] in self.prefix_chars:
IndexError: string index out of range
My ... |
string = "heLLo hOw are you toDay"
results = string.find("[A-Z]") <----- Here is my problem
string.lower().translate(table) <--- Just for the example.
>>>string
"olleh woh era uoy yadot"
#here i need to make the characters that where ...
|
Hey, ive been playing around with text-based RPGs, and im having trouble witha a loop. The lines giving me trouble are: Anenemy = (2, 1, 1, 0, 0, 0, 1, 1, 0, 1, 'an ememy', 1, 20) # defines stats of an enemy type enemy1 = Anenemy #defines the first enemy slot as anenemy. Exp = Exp + enemy1[12] # the ... |
def thousands(num): temp = "" rev = str(num)[::-1] for i in rev: temp += rev[i] if len(rev) > 3 and ((i+1) % 3 == 0): temp.join(',') wcom = temp[::-1] if wcom[0] == ',': ... |
>>> def get_odd_palindrome(word, index): ... lo, hi = index-1, index+1 ... while lo >= 0 and hi < len(word): ... if word[lo] != word[hi]: ... break ... lo, hi = lo-1, hi+1 ... return word[lo+1:hi] ... >>> |
import csv L = open("C:\\test.csv").read().split('\n') for line in L: # beginning of loop a = line[0] b = line[2] c = line[4] d = line[6] e = line[-1] print a, "ok", b, ... |
Hi All Is there any reason why string[x:y] returns a substring containing characters from index x to y-1 instead of from index x to y? I find this is very unintuitive. Thank you. and range(5, 10) is [5, 6, 7, 8, 9] and not [5,6,7,8,9,10] ? okay this is what the Python Tutorial says One way to remember how slices work ... |
Hello, i'm writing a rather simple script, the idea is to have two strings, one containing the letters of a word, the other containing underscores with the same length (think hangman). i want to replace one of the underscores with the corresponding letter, so i made a variable called location, and assigned it to string.find(letter) and then did a simple reassignment ... |
I'm writing a text adventure and working on making raw_input passable ultimately to functions that print the results of the user's input to the user. The problem I'm having is with the following code. Code: Select all #if bob=1 start at begining, if bob=2 load save. bob = [1] user_position = ['user_room'] user_says = [1] #define the command prompt. def prompt(): ... |
I'm doing an adressbook, it saves the contacts to a txt using the following pattern: Peter:Petrelli:545 Fake Av.:0695555966:1 Mohindler:Suresh:4433 Fake Av.:43567670:2 Claire:Bennet:343 Nowhere St.:34344405:3 This is: name:lastname:address:phone:id\n So as you can see I use an index number at the end of the line, and I'm doing a method to search for contacts, it will search for lines and once it founds ... |