I'd like to capitalize the first letter of a
&str. It's a simple problem and I hope for a simple solution. Intuition tells me to do something like this:
let mut s = "foobar";s = s.to_uppercase();
&strs can't be indexed like this. The only way I've been able to do it seems overly convoluted. I convert the
&str to an iterator, convert the iterator to a vector, upper case the first item in the vector, which creates an iterator, which I index into, creating an
Option, which I unwrap to give me the upper-cased first letter. Then I convert the vector into an iterator, which I convert into a
String, which I convert to a
let s1 = "foobar";let mut v: Vec<char> = s1.chars().collect();v = v.to_uppercase().nth(0).unwrap();let s2: String = v.into_iter().collect();let s3 = &s2;
Is there an easier way than this, and if so, what? If not, why is Rust designed this way?
Say I have a string word.
This string can change what characters it contains.
word = "UPPER£CASe"
How would I test the string to see if any character in the string is not uppercase. This string must only contain uppercase letters and no other punctuation, numbers, lowercase letters etc.
I was trying to make string's all character in uppercase but as string is immutable in python i am not getting this permanently in string. is there any function or alternative to get that?
message = "Have a great day" print(message.upper()) message.upper() print(message)
first print is giving all in caps but second is returning same string with no changes...
My question is simple: How to check if a String is more then 60% uppercase?
I want to use the *args to input the name and change the even numbers for the character to Upper Case and odd number character to lower case. For example,
I used the following code for the operation. But it is output all the characters as upper case. (If I add any random variable under myfunc1(x), then it is working properly.)
Using *args :
def myfunc1(*args): result='' for i in range(0,len(args)): if i%2==0: result+=args[i].upper() else: result+=args[i].lower() print(result)myfunc1('My Name') # prints "MY NAME"
Using random Variable:
def myfunc1(my_string): result='' for i in range(0,len(my_string)): if i%2==0: result+=my_string[i].upper() else: result+=my_string[i].lower() print(result)myfunc1('Myname') # prints MyNaMe