is there anyway to run a function inside a string? - python-3.x

I'm trying to run a function (that will give a random number) and display that in a string (btw this is part of a discord bot) and I'm wondering how I could do that
em = discord.Embed(title = "You Earned rndm",
color = discord.Color(value = 0x2ecc71))
'rndm' is my function and I'm wondering if there is a way for it to display the number that the function gives instead of text?

Just do the below
string = "text you want " + str(rndm()) + "more text"
Or
string = f"text you want {str(rndm())} and more text"
If your function returns a string you don't need the str() but it sounds like it returns an int

Related

How to remove text character values from string on flutter

I would like to retrieve only the number values from a string on flutter without keeping the text hardcoded using replaceAll, the text can be anything but the number part of it has to be retrieved from it.
e.g.
String text = "Hello your number is: 1234";
String numberProvided = '1234'; // needs to be extracted from String text
print("The number provided is :" + numberProvided);
Like I said, the text characters shouldn't be hardcoded into the application, let me know if it is possible, thanks!
Use the simple regular expression
print(text.replaceAll(RegExp("[a-zA-Z:\s]"), ""));
Try below code hope its help to you. refer replaceAll method here
void main() {
String text = "Hello your number is: 1234567890";
var aString = text.replaceAll(RegExp(r'[^0-9]'), '');
var aInteger = int.parse(aString);
print(
"The number provided is :" + aInteger.toString(),
);
}
Your Output:
The number provided is :1234567890

Random string picker (string names are exactly the same, except the number)

Dim rnd As New Random
Dim quote1, quote2, quote3 As String
Dim int As Integer
int = rnd.Next(1, 3)
quote1 = "never give up"
quote2 = "always believe in yourself"
quote3 = "always follow your dreams"
MessageBox.Show("quote" & int)
Hey, can somebody please tell me, how I can assign the int to the word quote, so every time it would pick a different quote?
With only 3 quotes you can do something like
Dim quoteIndex As Integer = Rnd.Next(1, 3)
Dim quote As String = ""
Select Case quoteIndex
Case 1
quote = quote1
Case 2
quote = quote2
Case 3
quote = quote3
End Select
MessageBox.Show(quote)
But in all honesty it's a quite lame solution, more akin to ninja code than to good practices. Instead, you should use an array or a list (which can be created inside this method or come from somewhere else like an overload or a modal variable):
Dim quoteList As New List(Of String)
quoteList.AddRange({"never give up", "always believe in yourself", "always follow your dreams", "something else"})
Dim quoteChoosen As Integer = Rnd.Next(0, quoteList.Count) 'this array start at zero
MessageBox.Show(quoteList(quoteChoosen)) '
If your list evolves over time (assuming that it's stored in a variable somewhere), your method won't need to be updated. Your user could add his own motivational quotes to the list without breaking your code, for an example.
As your code it written, you're showing a string value in the MessageBox. The string is being appended to, so it is dynamic and random, but it's still a string.
To get the affect I think you're looking for, you would need to use your random value as a pointer of some sort to reference a variable value. Using an array is probably the most straight-forward way to do that with this code. Instead of having 3 distinct quote strings values, you could create an array of strings...something like
quote = new string[]
where
quote[0] = "never give up"
then you could do something like MessageBox.Show(quote[int])

How to Split a string with a set of delimiters and find what delimiter it was? Kotlin

So I am learning Kotlin now, and I was trying to do a calculator where if we can give expression like 4+3 or 3*5 and we will get the answer so I was trying to split that input string and then find what operator is used and what are the operands.
var list = str.split("+","-","*","/" )
so how can i get the delimiter that is used to split that string too.
I'm afraid that split method doesn't have this feature. You would have to split the the string via separate split calls. And compare the outcome with original string. If the string wasn't split by given delimiter that outcome should be the same.
Eg. like this:
var str = "5+1"
var delimiters = arrayOf("+","-","*","/")
var found = "Not found"
for (delimiter in delimiters) {
var splited = str.split(delimiter)
if(splited[0] != str) {
found = delimiter
break
}
}
println(found)

Xcode 6.1 & Swift - textField input string to integer for basic math

I am slowly understanding things in swift, I am coming for a javascript background so it is somewhat familiar.
However variables are urking me.
in JS a variable can be
var varName = 1; //Number
var varName2 = "petey" //String
var conCat = varname + varName2; // 1petey
however in swift String vars and In var are troubling me. All I want to do is capture decimal number from user input from multiple "textField" sources and store that data to variable (which i already have setup) I need to concatinate a few of them with + then do basic math with the data in the variables with * and /.
How do I make the textFeld only capture?int or how do I convert text field strings to numbers, e.g. int?
A UITextField contains a String, not an Int, so you have to convert it, e.g.
let number : Int? = textField.text.toInt()
So, there actually is a method to convert from String to Int built-in in Swift. Beware, that it returns an optional, because the conversion may fail. So you have to check for nil before you use the variable.
For your other question, take a look at e.g. UITextField - Allow only numbers and punctuation input/keypad. Basically, you will have to adapt UITextField and filter it, once there are new entries. On iOS it might be easier, because you can show a numbers-only keyboard.
The data send from the textField is String. There are multiple ways to convert an Int to a String.
var a:String="\(2+3)" //"5"
And to concatenate a String to Int :
var b:String="Hello "+"\(3*4)" //"Hello 12"
And to Convert a String to an Int from a textField:
var b:Int=textField.text.toInt()
Use the function in swift inputmethod.intValue(); if any text is entered then it returns with a 0.

Actionscript 3 replacing string

I am trying to replace a substring in a string. My code below replaces all occurrences of substring. E.g. When clicked on in it replaces both in. But I like to replace only clicked one. How can we do this?
The books are in the table in my room.
function correct(e:TextEvent):void{
str =String(e.currentTarget.htmlText);
if(e.text==replacements[e.currentTarget.name]){
e.currentTarget.htmlText =strReplace(str, e.text, corrections[e.currentTarget.name]);
}
}
function strReplace(str:String, search:String, replace:String):String {
return str.split(search).join(replace);
}
You can use TextField.getCharIndexAtPoint(x:Number, y:Number):int to get the (0-based) index of the char in a particular coordinate.
You obviously need to use your clicked point as (x, y).
You can use localX and localY from TextField.click event.
See here for more: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html#getCharIndexAtPoint%28%29
At this point you need to search the string to replace in the neighborhood of the clicked char.
For example, something like this:
stringToReplace = "in";
len = stringToReplace.Length;
neighborhood = TextField.substring(clickedCharIndex-len, clickedCharIndex+len);
if (neighborhood.indexOf(stringToReplace)) {
neighborhood = neighborhood.replace(stringToReplace, replacement);
TextField.text = TextField.text(0, clickedCharIndex-len) + neighborhood +
TextField.text(clickedCharIndex+len);
}

Resources