Tagged Questions
0
votes
2answers
17 views
Findin a pattern in a string with loops in python
I'm trying to write a function that will determine whether a given pattern exists in a given string. For example "abc" in "afewtabcks"
To do this, I have written a main function that will prompt for ...
-2
votes
1answer
23 views
How do i find a string within a string in Python?
I'm working on a Mad-Libs assignment for class.
Essentially I need to isolate a {blank} in a string and print that as a prompt. I then need to return that input to where the blank used to be. This ...
0
votes
0answers
27 views
Solve system of a set number of equations
I'm attempting to make a code that will solve a set of nonlinear equations depending on a user input, n. Where n is the number of stages in a separation and with each n there are n+1 degrees of ...
0
votes
3answers
43 views
Python: list iteration works but single string doesn't
I'm very new to Python and am working my way through the "Learn Python The Hard Way" web tutorial. But I've come to a halt as I have an issue with passing a single string. I'm able to pass a list ...
-3
votes
3answers
52 views
How to extract a number from a string in Python?
How do you extract a number from a string to be able to manipulate it? The number could be either an int or a float. For example if the string is "flour, 100, grams" or "flour, 100.5, grams" then ...
-3
votes
0answers
32 views
Python Search for WHOLE words [duplicate]
I am running Python V2.7
I would like to pinpoint a whole word in a string, but I am having troubles. I have tried using the in statement but no luck. Heres and example:
if "word" in "Sword" # This ...
0
votes
3answers
24 views
How to combine variables with Python
This is my problem:
def encrypt(mult1, mult2):
encrypted = mult1 * mult2
print (encrypted)
encrypted_message = mult1, encrypted, mult2
print (encrypt_key)
return encrypt_key
...
0
votes
1answer
23 views
Python Conditional Password Program
I'm writing a simple conditional password checker script with the conditions being:
> 8 characters
< 24 characters
No special characters
At least two upper case letters
At least two lower case ...
-1
votes
1answer
37 views
Python simple string cycle
What it should look like:
enter word: word
enter number: 5
word
word+word
word+word+word
word+word+word+word
word+word+word+word+word
What I had:
for i in range(1,number+1):
print(word*i)
...
12
votes
1answer
276 views
How does \v differ from \x0b or \x0c?
Typing string.whitespace gives you a string containing all whitespace characters defined by Python's string module:
'\t\n\x0b\x0c\r '
Both \x0b and \x0c seem to give a vertical tab.
>>> ...
-3
votes
1answer
22 views
need help. python integers and strings [duplicate]
I have been having some issues with a line of code in a program I'm making. when ever I run the file it displays this message:
Traceback (most recent call last):
File "C:\Users\Main\Google ...
-4
votes
2answers
34 views
Python - How to get string in a Function?
I am currently writing a hang man game. Using Tkinter for GUI.
How can I get a string from a function:
def startgame():
player21abel=Label(text=" Guess a Letter ...
0
votes
2answers
25 views
Getting ambiguous truth value for string in for loop and not sure why
I have a list of strings called my_strings. I want to pull out all strings within that list that contain search_string
My attempt is the following:
new_strings = [my_str for my_str in my_strings if ...
0
votes
1answer
39 views
python string formatting {:d} vs %d on floating point number
I realise that this question could be construed as similar to others, so before I start, here is a list of some possible "duplicates" before everyone starts pointing them out. None of these seem to ...
-1
votes
1answer
19 views
Converting user input to binary and back in Python 3
I'm trying to create a program that will take a user's input, such as a URL that contains numbers, symbols, and letters and converts it into a string of binary. I've tried looking up string to binary ...
0
votes
6answers
47 views
Replacing parts of strings in a list in Python
I know similar questions exist for this topic but I've gone through them and still couldn't get it.
My python program retrieves a subsection of html from a page using a regular expression. I just ...
0
votes
1answer
55 views
Search and replace between two files with search phrase in first file: Python
File 1:
$def String_to_be_searched (String to be replaced with)
File 2:
..... { word { ${String to be searched} } # each line in File 2 is in this format
..... { word { ${String} } # This line ...
1
vote
2answers
56 views
python: how can I read elements of 2d dynamic lists?
I have following data
['1', '4', '4', '244', '263', '704', '952']
['2', '4', '4', '215', '172', '305', '33']
['3', '4', '4', '344', '279', '377', '1945']
['4', '4', '4', '66', '79', '169', '150']
...
-3
votes
1answer
27 views
Count Consonants Before First Vowel in a String
I'm creating a simple pig Latin translator. Here's what I have so far:
while True:
phrase = input('Translate > ').lower().split()
for word in phrase:
if word[0] in 'aeiou': #if the ...
0
votes
3answers
36 views
Python: Split string at the start of delimiter rather than the end
I'm trying to split a string using a multiple character delimiter, I can keep the delimiter in the result, but it is in the first part rather than the second part where I need it. This is what I have.
...
0
votes
1answer
22 views
i cannot get rid \xa0 in this string using python?
here is the code:
shipping_info = u'Expedited (2-3 Bus. Days)\xa0($17.99)'
I cannot get it to replace \xa0 using the method like replace.
here is what i tried:
x.replace('\\xa0', ' ')
...
-2
votes
0answers
36 views
Taking out contents from .docx file into txt file in python [on hold]
I am trying to read the contents from .docx file and write it into another.
for reading .docx file i did this
import zipfile
z = zipfile.ZipFile('abc.docx')
data = z.read('word/document.xml')
now ...
-4
votes
5answers
60 views
Python - split a string into sets of twos
I want to split a string into sets of twos
eg.
['abcdefg']
to
['ab','cd','ef']
thanks
here is what i had so far:
string = 'acabadcaa\ndarabr'
newString = []
for i in string:
...
-1
votes
1answer
27 views
Match repeating characters from set
from re import search
import random
while True:
r = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for _ in range(random.randint(1, 100)))
if ...
-2
votes
1answer
28 views
Python - Print last string after line.split
Is there a easy way to print the last string after you've split?
Input File:
Name:DOB:Age
Name:Name2:DOB:Age
I want the name and age...
So...
string = line.split(':')
print(string[0] + ...
-2
votes
3answers
26 views
How can I make a list of a input string the user has typed into Python?
I'm new in programming and in my script I have to create a list with a input string y the user.
I know how basic of slicing works and making a list ex: list = []
but I need to write the code so ...
0
votes
2answers
67 views
Capitalize the first letter in a string? [duplicate]
I have to to capitalize the first character of a word without lowering any letters.
I've tried using title() and capitalize() but they change the whole word by lowering the capitalized letter(s).
...
16
votes
1answer
1k views
How can I copy a Python string?
I do this:
a = 'hello'
And now I just want an independent copy of a:
import copy
b = str(a)
c = a[:]
d = a + ''
e = copy.copy(a)
map( id, [ a,b,c,d,e ] )
Out[3]:
[4365576160, 4365576160, ...
1
vote
1answer
26 views
Python: Cyrllic handling
I got this data returned b'\\u041a\\u0435\\u0439\\u0442\\u043b\\u0438\\u043d\\u043f\\u0440\\u043e from an API. This data is in Russian which I know for sure. I am guessing these values are unicode ...
1
vote
2answers
94 views
Remove word extension in python
I've got a text with several words. I want to remove all the derivative extension of the words. For example I want to remove extensions -ed -ing and keep the initial verb. If I i have the verifying or ...
-7
votes
3answers
10k views
Remove carriage return in python [closed]
I have following line:
line = "This is \nmy example"
and I'm using following code:
line = line.replace("\n", " ")
but it does not work
13
votes
2answers
15k views
How to sort the letters in a string alphabetically in Python
Is there an easy way to sort the letters in a string alphabetically in Python?
So for:
a = 'ZENOVW'
I would like to return:
'ENOVWZ'
11
votes
4answers
19k views
Why is datetime.strptime not working in this simple example?
I'm using strptime to convert a date string into a datetime. According to the linked page, formatting like this should work:
>>> # Using datetime.strptime()
>>> dt = ...
14
votes
3answers
35k views
Python string to unicode [duplicate]
Possible Duplicate:
How do I treat an ASCII string as unicode and unescape the escaped characters in it in python?
How do convert unicode escape sequences to unicode characters in a python ...
143
votes
8answers
131k views
Most elegant way to check if the string is empty in Python?
Does Python have something like an empty string variable where you can do?:
if myString == string.empty:
Regardless what's the most elegant way to check for empty string values? I find hardcoding ...
4
votes
6answers
9k views
Python string replace two things at once? [duplicate]
Say I have a string, "ab"
I want to replace "a" with "b" and "b" with "a" in one swoop.
So the end string should say "ba" and not "aa" or "bb" and not use more than one line. Is this doable?
7
votes
6answers
10k views
How to split a string into two integers in python
I have a string "42 0" (for example) and need to enter into an array the two integers. can i do a .split on a space?
thanks in advance
64
votes
4answers
80k views
TypeError: 'str' does not support the buffer interface
string = input("Please enter the text you want to compress")
file = input("Please enter the desired filename")
with gzip.open(file+".gz","wb") as f_out:
f_out.write(string)
The above python ...
13
votes
8answers
40k views
convert a string to an array
How do you convert a string into an array? say the string is like text = "a,b,c".
After the conversion, text == [a,b,c] and hopefully text[0] == a, text[1] == b?
Thank you
43
votes
3answers
76k views
Python: Extract numbers from a string
I would extract all the numbers contained in a string. Which is the better suited for the purpose, regular expressions or the isdigit() method?
Example:
line = "hello 12 hi 89"
Result:
[12, 89]
32
votes
9answers
5k views
Is there a generator version of `string.split()` in Python?
string.split() returns a list instance. Is there a version that returns a generator instead? Are there any reasons against having a generator version?
594
votes
5answers
450k views
Does Python have a string contains method?
I'm looking for a string.contains or string.indexof method in Python.
I want to do:
if not somestring.contains("blah"):
continue
9
votes
5answers
23k views
Multiple character replace with python
I need to replace some characters as follows : & -> \&, # -> \#, ...
I coded as follows, but I guess there should be some better way. Any hints?
strs = strs.replace('&', '\&')
strs = ...
323
votes
11answers
518k views
Python: read file line by line into array
How do I read every line of a file in Python and store each line as an element in an array?
I want to read the file line by line and each line is appended to the end of the array. I could not find ...
341
votes
7answers
272k views
How to trim whitespace (including tabs)?
Is there a function that will trim not only spaces for whitespace, but also tabs?
370
votes
3answers
175k views
Reverse a string in Python
There is no built in reverse function in Python's str object. What is the best way of implementing this?
If supplying a very concise answer, please elaborate on it's efficiency. Is the str converted ...
306
votes
5answers
280k views
Trimming a string in Python?
How do I remove leading and trailing whitespace from a string in Python?
For example:
" Hello " --> "Hello"
" Hello" --> "Hello"
"Hello " --> "Hello"
"Bob has a cat" --> "Bob has a ...
65
votes
8answers
177k views
How to convert strings into integers in python?
I have a tuple of tuples from MySQL query like this:
T1 = (('13', '17', '18', '21', '32'),
('07', '11', '13', '14', '28'),
('01', '05', '06', '08', '15', '16'))
I'd like to convert all ...
176
votes
6answers
154k views
Convert bytes to a Python string
I'm using this code to get standard output from an external program:
>>> from subprocess import *
>>> command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]
The ...
509
votes
13answers
797k views
Parse String to Float or Int
In Python, how can I parse a numeric string like "545.2222" to its corresponding float value, 542.2222? Or parse the string "31" to an integer, 31?
I just want to know how to parse a float string to ...