What is the Python 3 equivalent of %s in strings? - python-2.7

I've made some basic progress in python before, nothing more than command land algebra calculators to do math homework, using user-defined functions, inputs, and basic stuff. I've since taken the Python 2 course that codeacademy has, and I'm finding no equivalent of using % and %s for PY3.
I've narrowed it down to having some relation to format() , but that's as far as I could find on Google.
As a beginner, I'd really appreciate a watered down explanation to how to translate this into Python 3:
str1 = "Bob,"
str2 = "Marcey."
print "Hello %s hello %s" % (str1, str2)
EDIT: Also, I'm aware that print("Hello " + str1 + "hello " + str2) would work.

str.__mod__() continues to work in 3.x, but the new way of performing string formatting using str.format() is described in PEP 3101, and has subsequently been backported to recent versions of 2.x.
print("Hello %s hello %s" % (str1, str2))
print("Hello {} hello {}".format(str1, str2))

This should work as intended:
str1 = "Bob,"
str2 = "Marcey."
print("Hello {0} hello {1}".format(str1, str2))
While the use of % to format strings in Python 3 is still functional, it is recommended to use the new string.format(). It is more powerful and % will be removed from the language at some point.
Go on the Python website to see changes from Python 2.7 to Python 3 and the documentation contains everything you need.
:)

The % operator is not related to print; rather, it is a string operator. Consider this valid Python 2.x code:
x = "%s %s" % (a, b)
print x
Nearly identical code works in Python 3:
x = "%s %s" % (a, b)
print(x)
Your attempt would be correctly written as
print("%s %s" % (a, b))
The % operator is analogous to the C function sprintf, not printf.

Use f-strings in Python 3
prefix the string with f or F for variables and expressions inserted between curly braces inside the string to be evaluated inline:
str1 = "John Cleese"
str2 = "Michael Palin"
age1 = 73
age2 = 78
print(f"Hello {str1}, hello {str2}, your ages add to {age1 + age2}.")
Note the Python3 brackets in print(). Apparently, string interpolation is faster than the .format() syntax of Python 2.

The method you are using is still available in Python 3 (str.mod()).
In addition to this you can make use of string formatting in Python.
e.g:
print("This is {}".format("sparta")) #gives output
"This is sparta"
or
a = "Sparta"
b = "King"
print("This is {} and I am its {}".format(a,b)) #gives output
"This is Sparta and I am its King"

Related

I don't see where i am supposed to at the ':'

a = raw_input("What do you want to add to the list?"
" If you would like to remove something from the list type 'remove' : ")
if str(a) not == "remove".lower():
print('"'), str(a), ('"' " has been added to list")
list1.append(a)
print(list1)
When i run this (there is more code but this is the relevant bit) it says "expected ':'" just before the not in the "if str(a) not == "remove".lower():" line. keep in mind that this is on python 2.7
To test for inequality in Python this is valid:
A != B
and so is this:
not (A == B)
but this is not:
A not == B # syntax error
(You could be forgiven for thinking that's how it works if you've been using the in operator, because not (A in B) and A not in B are equivalent.)
Once you have fixed that, know that this:
print('"'), str(a), ('"' " has been added to list")
is cursed, and only accidentally does what you want in Python 2 (it won't do what you evidently assume it does in any currently-supported Python version). When you want to print() something, the specification of what is to be printed ends at that first ). The following works in both Python 2 and Python 3:
print( '"{}" has been added to list'.format(a) )
Then there's the fact that you are applying .lower() to the wrong side of a comparison (to something that is already always lower-case).

Text processing to get if else type condition from a string

First of all, I am sorry about the weird question heading. Couldn't express it in one line.
So, the problem statement is,
If I am given the following string --
"('James Gosling'/jamesgosling/james gosling) , ('SUN Microsystem'/sunmicrosystem), keyword"
I have to parse it as
list1 = ["'James Gosling'", 'jamesgosling', 'jame gosling']
list2 = ["'SUN Microsystem'", 'sunmicrosystem']
list3 = [ list1, list2, keyword]
So that, if I enter James Gosling Sun Microsystem keyword it should tell me that what I have entered is 100% correct
And if I enter J Gosling Sun Microsystem keyword it should say i am only 66.66% correct.
This is what I have tried so far.
import re
def main():
print("starting")
sentence = "('James Gosling'/jamesgosling/jame gosling) , ('SUN Microsystem'/sunmicrosystem), keyword"
splited = sentence.split(",")
number_of_primary_keywords = len(splited)
#print(number_of_primary_keywords, "primary keywords length")
number_of_brackets = 0
inside_quotes = ''
inside_quotes_1 = ''
inside_brackets = ''
for n in range(len(splited)):
#print(len(re.findall('\w+', splited[n])), "length of splitted")
inside_brackets = splited[n][splited[n].find("(") + 1: splited[n].find(")")]
synonyms = inside_brackets.split("/")
for x in range(len(synonyms)):
try:
inside_quotes_1 = synonyms[x][synonyms[x].find("\"") + 1: synonyms[n].find("\"")]
print(inside_quotes_1)
except:
pass
try:
inside_quotes = synonyms[x][synonyms[x].find("'") + 1: synonyms[n].find("'")]
print(inside_quotes)
except:
pass
#print(synonyms[x])
number_of_brackets += 1
print(number_of_brackets)
if __name__ == '__main__':
main()
Output is as follows
'James Gosling
jamesgoslin
jame goslin
'SUN Microsystem
SUN Microsystem
sunmicrosyste
sunmicrosyste
3
As you can see, the last letters of some words are missing.
So, if you read this far, I hope you can help me in getting the expected output
Unfortunately, your code has a logic issue that I could not figure it out, however there might be in these lines:
inside_quotes_1 = synonyms[x][synonyms[x].find("\"") + 1: synonyms[n].find("\"")]
inside_quotes = synonyms[x][synonyms[x].find("'") + 1: synonyms[n].find("'")]
which by the way you can simply use:
inside_quotes_1 = synonyms[x][synonyms[x].find("\x22") + 1: synonyms[n].find("\x22")]
inside_quotes = synonyms[x][synonyms[x].find("\x27") + 1: synonyms[n].find("\x27")]
Other than that, you seem to want to extract the words with their indices, which you can extract them using a basic expression:
(\w+)
Then, you might want to find a simple way to locate the indices, where the words are. Then, associate each word to the desired indices.
Example Test
# -*- coding: UTF-8 -*-
import re
string = "('James Gosling'/jamesgosling/james gosling) , ('SUN Microsystem'/sunmicrosystem), keyword"
expression = r'(\w+)'
match = re.search(expression, string)
if match:
print("YAAAY! \"" + match.group(1) + "\" is a match 💚💚💚 ")
else:
print('🙀 Sorry! No matches! Something is not right! Call 911 👮')

Python lstrip Sometimes Removes Extra Character [duplicate]

This question already has answers here:
Python string.strip stripping too many characters [duplicate]
(3 answers)
Closed 7 years ago.
I have Python 2.7 code that operates on a list of files. In part of the code I strip away the directory information. Today I was surprised to find that code didn't work correctly, when the file names begin with "s". This sample code demonstrates the problem:
import os
TEST_RESULTS_DIR = ".." + os.sep + "Test Results"
filename = TEST_RESULTS_DIR + os.sep + "p_file.txt"
stripped_filename = filename.lstrip(TEST_RESULTS_DIR + os.sep)
print ("%s : %s") % (filename, stripped_filename)
filename = TEST_RESULTS_DIR + os.sep + "s_file.txt"
stripped_filename = filename.lstrip(TEST_RESULTS_DIR + os.sep)
print ("%s : %s") % (filename, stripped_filename)
When I run this code, I get this:
..\Test Results\p_file.txt : p_file.txt
..\Test Results\s_file.txt : _file.txt
Does anyone understand why?
Lstrip doesn't replace a string at the beginning of another string, it strips all characters that match the characters in the string argument from the string it is called on.
For example:
"aaabbbc".lstrip("ba") = "c"
Your directory has an s in it, so it get's striped, you would see the same result if the file started with a u or an e.

C / C++ implement and pass code [duplicate]

This question already has answers here:
String literals C++?
(4 answers)
Closed 9 years ago.
I have interpreter from another language in C. I have to pass code (about 200 lines) from another language to this interpreter and here problem occur.
char* command_line[] = {
"",
"-e",
"print \"Hello from C!\\n\";"
};
This code is parse by:
(..)
parser(my_p, NULL, 3, command_line, (char **)NULL);
(...)
In code abobe I use array but even simple code have to be wrapped with \ before chars like " ; | \ etc.
How to avoid this problem and pass more then 200 multi row lines of code comfortable?
If you are using C++11, you can use raw string literals.
R"(print "Hello from C!\n";)"
Or you can simply put all the data into an external file and just read it on start. No need to escape any data there.
You could use the C preprocessor to do the stringification for you:
#define STRINGIFY(...) #__VA_ARGS__
#define STRINGIFY_NL(...) #__VA_ARGS__ "\n"
char* command_line[] = {
"",
"-e",
STRINGIFY(print "Hello from C!\n";), //< one element of the array
//< another element of the array
//< with embedded NL
STRINGIFY_NL(print "Hello from C!") //< no comma and the next line is glued
STRINGIFY ("for a second time";), //< end of other string
};
The only restrictions to observe would be that possible () have to balance inside the argument to STRINGIFY and that you'd have to place the macro on each line that you want to escape.
Unfortunately there is no support for literal strings or similar useful constructs in C, so if you want to write the interpreted code inside your C program you will have to be careful and escape quotes and slashes as you have stated.
The alternative is to write the code into a text file and treat it as an external resource file. You can read the resource file from inside your code into a string and then pass that to parser().
The ease of this depends on the platform you are using. Windows has good support for resource files and embedding them in to .exe files. I am sure it is possible with gcc too, but I haven't done it before. A bit vague I'm afraid, but I hope it helps.
You can use a script to take your text input, as a file, and stringify it (escaping double-quotes and newlines):
#!/usr/bin/env python
import sys
def main(argv = None):
if argv is None:
argv = sys.argv
if len(argv) < 2:
sys.stderr.write("Usage: stringify.py input.txt [... input.txt]\n")
sys.exit(1)
i = 1
while i < len(argv):
inname = argv[i]
firstline = True
try:
infile = open(inname, "r")
for line in infile:
line = line.replace('"', '\\"').replace('\n', '\\n')
sys.stdout.write('"{0}"\n'.format(line))
except IOError, msg:
sys.stderr.write("exception {0}\n".format(msg))
return 2
i = i + 1
return 0
if __name__ == "__main__":
sys.exit(main())

Pythonic way to rewrite the following C++ string processing code

Previous, I am having a C++ string processing code which is able to do this.
input -> Hello 12
output-> Hello
input -> Hello 12 World
output-> Hello World
input -> Hello12 World
output-> Hello World
input -> Hello12World
output-> HelloWorld
The following is the C++ code.
std::string Utils::toStringWithoutNumerical(const std::string& str) {
std::string result;
bool alreadyAppendSpace = false;
for (int i = 0, length = str.length(); i < length; i++) {
const char c = str.at(i);
if (isdigit(c)) {
continue;
}
if (isspace(c)) {
if (false == alreadyAppendSpace) {
result.append(1, c);
alreadyAppendSpace = true;
}
continue;
}
result.append(1, c);
alreadyAppendSpace = false;
}
return trim(result);
}
May I know in Python, what is the Pythonic way for implementing such functionality? Is regular expression able to achieve so?
Thanks.
Edit: This reproduces more accurately what the C++ code does than the previous version.
s = re.sub(r"\d+", "", s)
s = re.sub(r"(\s)\s*", "\1", s)
In particular, if the first whitespace in a run of several whitespaces is a tab, it will preserve the tab.
Further Edit: To replace by a space anyway, this works:
s = re.sub(r"\d+", "", s)
s = re.sub(r"\s+", " ", s)
Python has a lot of built-in functions that can be very powerful when used together.
def RemoveNumeric(str):
return ' '.join(str.translate(None, '0123456789').split())
>>> RemoveNumeric('Hello 12')
'Hello'
>>> RemoveNumeric('Hello 12 World')
'Hello World'
>>> RemoveNumeric('Hello12 World')
'Hello World'
>>> RemoveNumeric('Hello12World')
'HelloWorld'
import re
re.sub(r'[0-9]+', "", string)
import re
re.sub(r"(\s*)\d+(\s*)", lambda m: m.group(1) or m.group(2), string)
Breakdown:
\s* matches zero or more whitespace.
\d+ matches one or more digits.
The parentheses are used to capture the whitespace.
The replacement parameter is normally a string, but it can alternatively be a function which constructs the replacement dynamically.
lambda is used to create an inline function which returns whichever of the two capture groups is non-empty. This preserves a space if there was whitespace and returns an empty string if there wasn't any.
The regular expression answers are clearly the right way to do this. But if you're interested in a way to do if you didn't have a regex engine, here's how:
class filterstate(object):
def __init__(self):
self.seenspace = False
def include(self, c):
isspace = c.isspace()
if (not c.isdigit()) and (not (self.seenspace and isspace)):
self.seenspace = isspace
return True
else:
return False
def toStringWithoutNumerical(s):
fs = filterstate()
return ''.join((c for c in s if fs.include(c)))