Re: Iteration over strings
Jay Loden schrieb:
Robert Dailey wrote:
str = "C:/somepath/folder/file.txt"
for char in str:
if char == "\\":
char = "/"
strings in Python are immutable - in other words, they cannot be updated in place as you're doing above. However, that doesn't mean you can't achieve what you're after. In Python, the way to approach this problem since you cannot modify the string in place, is to create a new string object with the desired content:
Also note, that you are just extracting one char from the string
into another variable (which you then change) and you are *not*
getting any "reference" to the char in the string.
As has been alreagy told, use the str.replace() function
.
Relevant Pages
- Re: help getting substring
... I need a program to extract a substring from the following pattern ... char* deangle ... What does deangle do when passed a string of the form ... char *deangle(char *str) ... (comp.lang.c) - Re: How to Return an array of strings in C (char**)
... If your code in the calling function really uses a char* that points ... to a literal string, change it to use an array. ... any user defined function or object name beginning with str. ... ptr is a char**. ... (comp.lang.c) - Re: How to add thousand separators
... First, this code is obsolete as written, because char is a dead data type and should not ... Note that both of these should be stored as string resources since they might need to be ... 18 digits for any reason. ... you have made a VERY SERIOUS DESIGN ERROR. ... (microsoft.public.vc.mfc) - Re: what is the best way of passing floats into a string
... I do not null-terminate as snprintf takes care of this (according to ... But the easiest way to determine the size needed to format a number, ... int length_of_representation(double n,const char* format){ ... I get a nice result of -10.000000 in my char * string. ... (comp.unix.programmer) - Re: weird problem
... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ... (comp.lang.c) |
|