This question already has answers here:
Python: Delete a character from a string
(3 answers)
Closed 8 years ago.
Let's say I have a string "puete" and I want to delete the last "e", how can I do it?
I have used del[len(str)-1] but there is error showing 'str does not support item deletion'.
str objects don't support item deletion because they're immutable. The closest you can do is to make a new string w/out the last character by slicing the string:
In [20]: 'puete'[:-1]
Out[20]: 'puet'
Strings are immutable, i.e. can't be changed in-place; you cannot delete a single character. Instead, create a new string and assign it back to the same name:
s = "puete"
s = s[:-1]
The slice [:-1] means "up to but not including the last character", and will create a new string object with fewer characters.
Related
This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 1 year ago.
I am trying to make python take a string, separate the characters before another character eg: "10001001010Q1002000293Q100292Q". I want to separate the string before each Q and have python create either a list or another string. I cannot figure this out for the life of me.
You can do this using the split function, give "Q" as a parameter to the split function then you can slice the list to only get the numbers before Q.
num = "10001001010Q1002000293Q100292Q"
print(num.split("Q")[:-1])
Split() function: https://www.w3schools.com/python/ref_string_split.asp
Slicing: https://www.w3schools.com/python/python_strings_slicing.asp
The syntax is str.split("separator").
str = str.split("Q")
Then output will be ['10001001010', '1002000293', '100292', ''].
If you don't need the last empty element then you can write as:
str = str.split("Q")[:-1]
This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 4 years ago.
I'm new to Python and I'm attempting to place each individual character of a string into an individual element of an array.
string= 'Hello'
array= []
length_of_string= len(string)-1
for i in range (length_of_string):
array.append(string(i))
print(array)
However when I run this code an error occurs as shown below.
array.append(string(i))
TypeError: 'str' object is not callable
The append function works fine when I append to an array using strings or numbers normally but in this instance it does not work.
What do I have to do to get
['H','e','l','l','o']
You mean string[i] if you wan't the ith element of string (not string(i) -- python isn't matlab). However, it's much faster just to do
list(string) # ['H','e','l','l','o']
This question already has answers here:
How to detect string byte encoding?
(4 answers)
What is the difference between encode/decode?
(7 answers)
Closed last month.
I have a list of strings that originally was a list of unicode element. And so, some string contains some character accent in unicode formate.
list=['Citt\xe0','Veszpr\xe9m','Publicit\xe0']
I need to get a new list that looks like this:
new_list=[u'Citt\xe0',u'Veszpr\xe9m',u'Publicit\xe0']
Each element of the new_list has to carry both the u and the accent.
Is there a way to do it iterating on each element?
new_list=[unicode(repr(word)) for word in old_list]
>>> print new_list
[u"'Citt\\xe0'", u"'Hello'", u"'Publicit\\xe0'"]
Is that what you want?
This question already has answers here:
How to create a "singleton" tuple with only one element
(4 answers)
Closed 6 years ago.
x=([1,2,3])
type(x)= List
x=([1,2,3],[4,5,6])
type(x)=tuple
why does the type change?
The proper syntax for creating a tuple with only one item is to follow the item with a comma:
x=([1,2,3],)
which for this example will in fact give
type(x)=tuple
Reference the official Python 2 documentation
which states (quote)
A special problem is the construction of tuples containing 0 or 1
items: the syntax has some extra quirks to accommodate these. Empty
tuples are constructed by an empty pair of parentheses; a tuple with
one item is constructed by following a value with a comma (it is not
sufficient to enclose a single value in parentheses).
This question already has answers here:
Converting a string to a tuple in python
(3 answers)
Closed 8 years ago.
I downloaded a csv table from a database using SQL. One of the fields has values like so:
'[0.1234,0.0,0.0]'
I want to convert this string to a python list to get the first value. I only know how to convert strings to ints and floats... is there any way to de-string this object? The table I got from SQL is from a web-based viewer, I'm not getting it from my command line.
You could take the substring from index 1 to index -1 and then split it using the comma as a delimiter. In python
array = variable[1:-1].split(',')
should work.
If you're sure it is always valid list syntax, you could use"
myList = eval('[0.1234,0.0,0.0]')
Or if the value itself has quotes ' in it, you can slice those off
value = "'[0.1234,0.0,0.0]'"
myList = eval(value[1:-1])
Then to get the first value you just
myList[0]