Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

True for all characters of a string

In Python 3, what's the shortest way to check whether a predicate is true for all characters of a string?

like image 460
rwallace Avatar asked Feb 13 '13 17:02

rwallace


People also ask

Does a string have all unique characters?

If the value does not match for all the pairs of characters in a string, then the string has all unique characters. Otherwise, it does not.

How do you find all characters in a string are same?

To find whether a string has all the same characters. Traverse the whole string from index 1 and check whether that character matches the first character of the string or not. If yes, then match until string size. If no, then break the loop.

What are strings in character?

Character strings are the most commonly used data types. They can hold any sequence of letters, digits, punctuation, and other valid characters. Typical character strings are names, descriptions, and mailing addresses.

How many characters does a string have?

Therefore, the maximum length of String in Java is 0 to 2147483647. So, we can have a String with the length of 2,147,483,647 characters, theoretically.


2 Answers

all(predicate(x) for x in string)
like image 95
Abe Karplus Avatar answered Oct 28 '22 15:10

Abe Karplus


all(map(predicate, string))

Functionally the same as @Abe's answer but with map instead (also lazy in python3)

like image 20
SlimJim Avatar answered Oct 28 '22 14:10

SlimJim