how should I define a function that takes in a string and returns the string in upper and lowercases according to even and odd indexing?
def myfunc(string):
for some in string:
if string.index%2==0:
I have written this much but I do not know what should I type now.
please help.
Here you can find the string methods Specially look at upper() and lower() which will be your friend here.
Related
This is the part of the code I have copied to see the output,
def check(string,sub_str):
if(string.find(sub_str)==-1):
print('no')
else:
print('yes)
# driver code for testing the above function
string='geeks for geeks'
sub_str='geeks'
I specifically wanted to understand how this expression works :
if(string.find(sub_str)==-1): . Also this code is for finding substrings in a given strings can some one tell if this is the optimal way, I know it is tutorial code but I have an easier way to find the substrings. Just wanted to know if that would make passing test cases easier hence the above code. Anyways thanks y'all for your answers.
The method find() returns the index of the string you are looking for. The string in front of find() is the one in which you are looking for the second string.
SentenceThatIsCompletelySearched.find(ForThisPartHere)
If the string you are looking for is present it will return the index (a number on which position of the sentence the string has been found).
If the string is not inside the sentence then find() will return -1 (a number).
So in your case you are checking if sub_str is inside string and if it is not present (return of -1) you will print "no". If it is you will print "yes".
I have a long string "AB100123485;AB10064279293-IP-1-KNPO;AB473898487-MM41". I have to extract integer value after "IP-" i.e 1 (only) what is the most efficient way ? I am using c#
Thanks
The 'most-efficient' way depends on how consistent your string is in terms of length and appearance. You can surely do this with a regular expression as a quick solution if you just want to get the digit directly following IP-.
You can utilize the RegularExpressions API, passing in your regular expression and input string.
https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.match?view=netframework-4.8#System_Text_RegularExpressions_Regex_Match_System_String_System_String_
This pattern should get you started IP-[0-9]; refine it more to your use case as needed.
For example:
Match matched = System.Text.RegularExpressions.Match(
"AB100123485;AB10064279293-IP-1-KNPO;AB473898487-MM41",
"IP-[0-9]"
);
How can I divide a string with dots as delimiters in Groovy?
If I have a string like "22112018", how do I convert it to "22.11.2018"?
EDIT:
I wasn't really sure how to formulate the question. I wanted to 'split' the string but split() method doesn't do what I need (doesn't mean the same).
This answer in comments (by #ernest_k) was good enough for what I needed:
text = "22112018"
"${text[0..1]}.${text[2..3]}.${text[4..7]}"
However, it was not an "answer" in the SO way, so I'm accepting the answer by #tim_yates (also works and is probably a more precise and robust solution).
I assume this is a date...
You could do:
Date.parse('ddMMyyyy', '22112018').format('dd.MM.yyyy')
instead of just grabbing characters
Hey guys so I tried looking at previous questions but they dont answer it like my teacher wants it to be answered. Basically i need to get a string from a user input and see if it has:
at least one of [!,#,#,$,%,^,&,*,(,)] (non-letter and nonnumeric
character)
o Create a list for these special characters
I have no idea how to make a def to do this. Please help!
You should probably look into Regular expressions. Regular expressions allow you to do many string operations in a concise way. Specifically, you'll want to use re.findall() in order to find all special characters in your string and return them. You can check if the returned list has length 0 to check if there were any special characters at all.
With regards to building the regular expression to find special characters itself... I'm sure you can figure that part out ;)
Please try the below
import re
inputstring = raw_input("Enter String: ")
print inputstring
print "Valid" if re.match("^[a-zA-Z0-9_]*$", inputstring) else "Invalid"
I need to search for a substring in a string and return that if it is there in the string.
What is the best way to do that in Erlang? Note that i dont know the place that substring happens in the bigger string so i need to do a search for that.
You can use a regular expression:
> re:run("foobarbaz", "bar", [{capture, first, list}]).
{match,["bar"]}
See the documentation for re:run/3 for more information. In particular you may find that a different capture option suits your need.
Or if you don't need all the features of regular expressions, string:str/2 might be enough:
> string:str(" Hello Hello World World ", "Hello World").
8
This small function may help you. It returns true if the small string can be found in the big string, otherwise it returns false.
string_contains(Big, Small)->
string:str(Big, Small) > 0.