Error : can only concatenate str (not "int") to str - python-3.x

please help I can't fix this one I'm stuck here for 2 hours ago I can't pass this
when I input only one username it's ok but if I input more then one username it's an error like the title
I want output like this

Your problem is that you are replacing your counter (cnt) with a string:
cnt = str(cnt)
Use a different variable name for the string variable, such as:
cnt_str = str(cnt)

Related

I can't get two String values in one string using templates

I got some function:
private fun selectHometown() = File("data/towns.txt")
.readText()
.split("\n")
.shuffled()
.first()
And if I try to get or print some string with the 2 values obtained from this function, the first value disappears. For example:
println("${selectHometown() ${selectHometown() }")
Will only print one city name, while I expect two. I guess the problem is related to string concatenation in Kotlin. Of course, I can get the desired result in a different way, but I'm wondering why this one doesn't work.
Windows way of terminating a line is to use "\r\n" so use it as delimiter :
private fun selectHometown() = File("data/towns.txt")
.readText()
.split("\r\n")
.shuffled()
.first()
println("${selectHometown()} ${selectHometown()}")

Finding substring within string

I want to find a specific string within a string.
For example, let's say I have the string
string = "username:quantopia;password:blabla
How can I then find quantopia?
I am using python 3.
Update: I am sorry I did not mention what I try before..
string.split('username:',1)[1].split(';',1)[0]
But this look very bad and not efficient, I was hoping for something better.
Just use regex as such:
import re
username = re.search("username:(.*);password", "username:quantopia;password:blabla").group(1)
print("username:", username)
This will output quantopia.
In this expression "username:(.*);password" you are saying "give me everything from username: to ;password" So this is why you're getting quantopia. This might as well be ":(.*);" as it will output the same thing in this case.
The simple solution is:
string = "username:quantopia;password:blabla"
username = "username"
if username in string:
# do work.
You might be better to just use split to create a dictionary so you dont need to use multiple regex to extract different parts of data sets. The below will split stirng into key value pairs then split key value pairs then pass the list of lists to dict to create a dictionary.
string = "username:quantopia;password:blabla"
data = dict([pairs.split(':') for pairs in string.split(';')])
print(f'username is "{data["username"]}" and password is "{data["password"]}"')
OUTPUT
username is "quantopia" and password is "blabla"

python3 replace ' in a string

I am trying to clean text strings containing any ' or &#39 (which includes an ; but if i add it here you will see just ' again. Because the the ANSI is also encoded by stackoverflow. The string content contains ' and when it does there is an error.
when i insert the string to my database i get this error:
psycopg2.ProgrammingError: syntax error at or near "s"
LINE 1: ...tment and has commenced a search for mr. whitnell's
the original string looks like this:
...a search for mr. whitnell&#39s...
To remove the ' and &#39 ; I use:
stripped_content = stringcontent.replace("'","")
stripped_content = stringcontent.replace("&#39 ;","")
any advice is welcome, best regards
When you try to replace("&#39 ;","") it literally searching for "&#39 ;" occurrences in string. You need to convert "&#39 ;" to its character equivalent. Try this:
s = "That's how we 'roll"
r = s.replace(chr(int('&#39'[2:])), "")
and with this chr(int('&#39'[2:])) you'll get ' character.
Output:
Thats how we roll
Note
If you try to run this s.replace(chr(int('&#39'[2:])), "") without saving your result in variable then your original string would not be affected.

How can I use scope_varname in IDL with a string construct representing the variable name?

I have an IDL function that takes in up to 4 data variables: data1, data2, data3 and data4. I want to be able to access the level=-1 scope of these variables in a loop using a string construct for the data variable name, so I can document the name of the original data that was passed to the function in an efficient manner.
Here's a simplified version of the function, showing only pertinent parts.
Function funcData, dat1, dat2, dat3, dat4,
n=1
txt = "Data "
;Check that data variable n was passed.
WHILE N_ELEMENTS(scope_varfetch("dat"+strtrim(n+1,1), level=0, /enter)) $
NE 0 DO BEGIN
dat = scope_varfetch("dat"+strtrim(n,1), level=0, /enter) ; get data
txt=txt + scope_varname("dat"+ strtrim(n,1), level=-1) +", " ; data names
n+=1 ; update n
ENDWHILE
END
The problem is that scope_varfetch handles the concatenated string construct "dat"+strtrim(n,1) and returns the appropriate data set, but scope_varname does not, returning a blank.
Does anyone know why this is happening?
Is there another way I can do this (short of brute force, case format)?
I have tried to search for an answer on-line, but have not been able to find anything about using string constructs in the IDL scope functions.
A Facebook contact provided this solution:
result=execute('sv=scope_varname(dat'+ strtrim(n,1)+', level=-1)')
txt=txt + sv + ", "
Works perfectly.
scope_varname expects a variable as its parameter, so you need an extra call to scope_varfetch when using it:
txt=txt + scope_varname((scope_varfetch("dat"+ strtrim(n,1))), level=-1) +", " ; data names

Swift2 - Split String to individual characters

I am trying to split a string into individual characters.
The string I want to split: let lastName = "Kocsis" so that is returns something like: ["K","o","c","s","i","s"]
So far I have tried:
var name = lastName.componentsSeparatedByString("")
This returns the original string
name = lastName.characters.split{$0 == ""}.map(String.init)
This gives me an error: Missing argument for parameter #1 in call. So basically it does't accept "" as an argument.
name = Array(lastName)
This does't work in Swift2
name = Array(arrayLiteral: lastName)
This doesn't do anything.
How should I do this? Is There a simple solution?
Yes, there is a simple solution
let lastName = "Kocsis"
let name = Array(lastName.characters)
The creation of a new array is necessary because characters returns String.CharacterView, not [String]

Resources