Description
stringlengths
9
105
Link
stringlengths
45
135
Code
stringlengths
10
26.8k
Test_Case
stringlengths
9
202
Merge
stringlengths
63
27k
Python program to print even length words in a string
https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/
# Python code # To print even length words in string # input string n = "This is a python language" # splitting the words in a given string s = n.split(" ") for i in s: # checking the length of words if len(i) % 2 == 0: print(i) # this code is contributed by gangarajula laxmi
Input: s = "This is a python language" Output: This is python language
Python program to print even length words in a string # Python code # To print even length words in string # input string n = "This is a python language" # splitting the words in a given string s = n.split(" ") for i in s: # checking the length of words if len(i) % 2 == 0: print(i) # this code is contributed by gangarajula laxmi Input: s = "This is a python language" Output: This is python language [END]
Python program to print even length words in a string
https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/
# Python3 program to print # even length words in a string def printWords(s): # split the string s = s.split(" ") # iterate in words of string for word in s: # if length is even if len(word) % 2 == 0: print(word) # Driver Code s = "i am muskan" printWords(s)
Input: s = "This is a python language" Output: This is python language
Python program to print even length words in a string # Python3 program to print # even length words in a string def printWords(s): # split the string s = s.split(" ") # iterate in words of string for word in s: # if length is even if len(word) % 2 == 0: print(word) # Driver Code s = "i am muskan" printWords(s) Input: s = "This is a python language" Output: This is python language [END]
Python program to print even length words in a string
https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/
# Python code # To print even length words in string # input string n = "geeks for geek" # splitting the words in a given string s = n.split(" ") l = list(filter(lambda x: (len(x) % 2 == 0), s)) print(l)
Input: s = "This is a python language" Output: This is python language
Python program to print even length words in a string # Python code # To print even length words in string # input string n = "geeks for geek" # splitting the words in a given string s = n.split(" ") l = list(filter(lambda x: (len(x) % 2 == 0), s)) print(l) Input: s = "This is a python language" Output: This is python language [END]
Python program to print even length words in a string
https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/
# python code to print even length words n = "geeks for geek" s = n.split(" ") print([x for x in s if len(x) % 2 == 0])
Input: s = "This is a python language" Output: This is python language
Python program to print even length words in a string # python code to print even length words n = "geeks for geek" s = n.split(" ") print([x for x in s if len(x) % 2 == 0]) Input: s = "This is a python language" Output: This is python language [END]
Python program to print even length words in a string
https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/
# python code to print even length words n = "geeks for geek" s = n.split(" ") print([x for i, x in enumerate(s) if len(x) % 2 == 0])
Input: s = "This is a python language" Output: This is python language
Python program to print even length words in a string # python code to print even length words n = "geeks for geek" s = n.split(" ") print([x for i, x in enumerate(s) if len(x) % 2 == 0]) Input: s = "This is a python language" Output: This is python language [END]
Python program to print even length words in a string
https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/
# recursive function to print even length words def PrintEvenLengthWord(itr, list1): if itr == len(list1): return if len(list1[itr]) % 2 == 0: print(list1[itr]) PrintEvenLengthWord(itr + 1, list1) return str = "geeks for geek" l = [i for i in str.split()] PrintEvenLengthWord(0, l)
Input: s = "This is a python language" Output: This is python language
Python program to print even length words in a string # recursive function to print even length words def PrintEvenLengthWord(itr, list1): if itr == len(list1): return if len(list1[itr]) % 2 == 0: print(list1[itr]) PrintEvenLengthWord(itr + 1, list1) return str = "geeks for geek" l = [i for i in str.split()] PrintEvenLengthWord(0, l) Input: s = "This is a python language" Output: This is python language [END]
Python program to print even length words in a string
https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/
s = "geeks for geek" # Split the string into a list of words words = s.split(" ") # Use the filter function and lambda function to find even length words even_length_words = list(filter(lambda x: len(x) % 2 == 0, words)) # Print the list of even length words print(even_length_words) # This code is contributed by Edula Vinay Kumar Reddy
Input: s = "This is a python language" Output: This is python language
Python program to print even length words in a string s = "geeks for geek" # Split the string into a list of words words = s.split(" ") # Use the filter function and lambda function to find even length words even_length_words = list(filter(lambda x: len(x) % 2 == 0, words)) # Print the list of even length words print(even_length_words) # This code is contributed by Edula Vinay Kumar Reddy Input: s = "This is a python language" Output: This is python language [END]
Python program to print even length words in a string
https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/
import itertools s = "geeks for geek" # Split the string into a list of words words = s.split(" ") # Use the itertools.filterfalse function and lambda function to find even length words even_length_words = list(itertools.filterfalse(lambda x: len(x) % 2 != 0, words)) # Print the list of even length words print(even_length_words)
Input: s = "This is a python language" Output: This is python language
Python program to print even length words in a string import itertools s = "geeks for geek" # Split the string into a list of words words = s.split(" ") # Use the itertools.filterfalse function and lambda function to find even length words even_length_words = list(itertools.filterfalse(lambda x: len(x) % 2 != 0, words)) # Print the list of even length words print(even_length_words) Input: s = "This is a python language" Output: This is python language [END]
Python program to print even length words in a string
https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/
# code def print_even_length_words(s): words = s.replace(",", " ").replace(".", " ").split() for word in words: if len(word) % 2 == 0: print(word, end=" ") s = "geeks for geek" print_even_length_words(s)
Input: s = "This is a python language" Output: This is python language
Python program to print even length words in a string # code def print_even_length_words(s): words = s.replace(",", " ").replace(".", " ").split() for word in words: if len(word) % 2 == 0: print(word, end=" ") s = "geeks for geek" print_even_length_words(s) Input: s = "This is a python language" Output: This is python language [END]
Python program to print even length words in a string
https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/
# Initialize the input string s = "This is a python language" # Initialize an empty string variable to store the current word being parsed word = "" # Iterate over each character in the input string for i in s: # If the character is a space, it means the current word has ended if i == " ": # Check if the length of the current word is even and greater than 0 if len(word) % 2 == 0 and len(word) != 0: # If yes, print the current word print(word, end=" ") # Reset the current word variable for the next word word = "" # If the character is not a space, it means the current word is being formed else: # Append the character to the current word variable word += i # After the loop has ended, check if the length of the last word is even and greater than 0 if len(word) % 2 == 0 and len(word) != 0: # If yes, print the last word print(word, end=" ")
Input: s = "This is a python language" Output: This is python language
Python program to print even length words in a string # Initialize the input string s = "This is a python language" # Initialize an empty string variable to store the current word being parsed word = "" # Iterate over each character in the input string for i in s: # If the character is a space, it means the current word has ended if i == " ": # Check if the length of the current word is even and greater than 0 if len(word) % 2 == 0 and len(word) != 0: # If yes, print the current word print(word, end=" ") # Reset the current word variable for the next word word = "" # If the character is not a space, it means the current word is being formed else: # Append the character to the current word variable word += i # After the loop has ended, check if the length of the last word is even and greater than 0 if len(word) % 2 == 0 and len(word) != 0: # If yes, print the last word print(word, end=" ") Input: s = "This is a python language" Output: This is python language [END]
Python program to print even length words in a string
https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/
from itertools import compress n = "This is a python language" s = n.split(" ") even_len_bool = [len(word) % 2 == 0 for word in s] even_len_words = list(compress(s, even_len_bool)) print(even_len_words)
Input: s = "This is a python language" Output: This is python language
Python program to print even length words in a string from itertools import compress n = "This is a python language" s = n.split(" ") even_len_bool = [len(word) % 2 == 0 for word in s] even_len_words = list(compress(s, even_len_bool)) print(even_len_words) Input: s = "This is a python language" Output: This is python language [END]
Python program to print even length words in a string
https://www.geeksforgeeks.org/python-program-to-print-even-length-words-in-a-string/
s = "I am omkhar" word_start = 0 even_words = [] for i in range(len(s)): if s[i] == " ": word_end = i word_length = word_end - word_start if word_length % 2 == 0: even_words.append(s[word_start:word_end]) word_start = i + 1 word_length = len(s) - word_start if word_length % 2 == 0: even_words.append(s[word_start:]) for w in even_words: print(w)
Input: s = "This is a python language" Output: This is python language
Python program to print even length words in a string s = "I am omkhar" word_start = 0 even_words = [] for i in range(len(s)): if s[i] == " ": word_end = i word_length = word_end - word_start if word_length % 2 == 0: even_words.append(s[word_start:word_end]) word_start = i + 1 word_length = len(s) - word_start if word_length % 2 == 0: even_words.append(s[word_start:]) for w in even_words: print(w) Input: s = "This is a python language" Output: This is python language [END]
Python - Uppercase Half String
https://www.geeksforgeeks.org/python-uppercase-half-string/
# Python3 code to demonstrate working of # Uppercase Half String # Using upper() + loop + len() # initializing string test_str = "geeksforgeeks" # printing original string print("The original string is : " + str(test_str)) # computing half index hlf_idx = len(test_str) // 2 res = "" for idx in range(len(test_str)): # uppercasing later half if idx >= hlf_idx: res += test_str[idx].upper() else: res += test_str[idx] # printing result print("The resultant string : " + str(res))
#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE
Python - Uppercase Half String # Python3 code to demonstrate working of # Uppercase Half String # Using upper() + loop + len() # initializing string test_str = "geeksforgeeks" # printing original string print("The original string is : " + str(test_str)) # computing half index hlf_idx = len(test_str) // 2 res = "" for idx in range(len(test_str)): # uppercasing later half if idx >= hlf_idx: res += test_str[idx].upper() else: res += test_str[idx] # printing result print("The resultant string : " + str(res)) #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]
Python - Uppercase Half String
https://www.geeksforgeeks.org/python-uppercase-half-string/
# Python3 code to demonstrate working of # Uppercase Half String # Using list comprehension + join() + upper() # Initializing string test_str = "geeksforgeeks" # Printing original string print("The original string is : " + str(test_str)) # Computing half index hlf_idx = len(test_str) // 2 # join() used to create result string res = "".join( [ test_str[idx].upper() if idx >= hlf_idx else test_str[idx] for idx in range(len(test_str)) ] ) # Printing result print("The resultant string : " + str(res))
#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE
Python - Uppercase Half String # Python3 code to demonstrate working of # Uppercase Half String # Using list comprehension + join() + upper() # Initializing string test_str = "geeksforgeeks" # Printing original string print("The original string is : " + str(test_str)) # Computing half index hlf_idx = len(test_str) // 2 # join() used to create result string res = "".join( [ test_str[idx].upper() if idx >= hlf_idx else test_str[idx] for idx in range(len(test_str)) ] ) # Printing result print("The resultant string : " + str(res)) #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]
Python - Uppercase Half String
https://www.geeksforgeeks.org/python-uppercase-half-string/
# Python3 code to demonstrate working of # Uppercase Half String # Using upper() + slicing string # initializing string test_str = "geeksforgeeks" # printing original string print("The original string is : " + str(test_str)) # computing half index hlf_idx = len(test_str) // 2 # Making new string with half upper case # Using slicing # slicing takes one position less to ending position provided res = test_str[:hlf_idx] + test_str[hlf_idx:].upper() # printing result print("The resultant string : " + str(res))
#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE
Python - Uppercase Half String # Python3 code to demonstrate working of # Uppercase Half String # Using upper() + slicing string # initializing string test_str = "geeksforgeeks" # printing original string print("The original string is : " + str(test_str)) # computing half index hlf_idx = len(test_str) // 2 # Making new string with half upper case # Using slicing # slicing takes one position less to ending position provided res = test_str[:hlf_idx] + test_str[hlf_idx:].upper() # printing result print("The resultant string : " + str(res)) #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]
Python - Uppercase Half String
https://www.geeksforgeeks.org/python-uppercase-half-string/
# Python3 code to demonstrate working of # Uppercase Half String # initializing string test_str = "geeksforgeeks" # printing original string print("The original string is : " + str(test_str)) # computing half index x = len(test_str) // 2 a = test_str[x:] b = test_str[:x] cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" sma = "abcdefghijklmnopqrstuvwxyz" res = "" for i in a: res += cap[sma.index(i)] res = b + res # printing result print("The resultant string : " + str(res))
#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE
Python - Uppercase Half String # Python3 code to demonstrate working of # Uppercase Half String # initializing string test_str = "geeksforgeeks" # printing original string print("The original string is : " + str(test_str)) # computing half index x = len(test_str) // 2 a = test_str[x:] b = test_str[:x] cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" sma = "abcdefghijklmnopqrstuvwxyz" res = "" for i in a: res += cap[sma.index(i)] res = b + res # printing result print("The resultant string : " + str(res)) #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]
Python - Uppercase Half String
https://www.geeksforgeeks.org/python-uppercase-half-string/
import re # initializing string test_str = "geeksforgeeks" # printing original string print("The original string is : " + str(test_str)) # computing half index hlf_idx = len(test_str) // 2 # Making new string with half upper case using regular expressions res = re.sub(r"(?i)(\w+)", lambda m: m.group().upper(), test_str[hlf_idx:]) # concatenating the two parts of the string result = test_str[:hlf_idx] + res # printing result print("The resultant string : " + str(result))
#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE
Python - Uppercase Half String import re # initializing string test_str = "geeksforgeeks" # printing original string print("The original string is : " + str(test_str)) # computing half index hlf_idx = len(test_str) // 2 # Making new string with half upper case using regular expressions res = re.sub(r"(?i)(\w+)", lambda m: m.group().upper(), test_str[hlf_idx:]) # concatenating the two parts of the string result = test_str[:hlf_idx] + res # printing result print("The resultant string : " + str(result)) #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]
Python - Uppercase Half String
https://www.geeksforgeeks.org/python-uppercase-half-string/
# Python3 code to demonstrate working of # Uppercase Half String # Using string concatenation and conditional operator # Iializing string test_str = "geeksforgeeks" # Printing original string print("The original string is : " + str(test_str)) # Computing half index length = len(test_str) hlf_idx = length // 2 # Initializing result string result = "" # Iterating through characters for i in range(length): # Conditional operator result += test_str[i] if i < hlf_idx else test_str[i].upper() # Printing result print("The resultant string : " + str(result))
#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE
Python - Uppercase Half String # Python3 code to demonstrate working of # Uppercase Half String # Using string concatenation and conditional operator # Iializing string test_str = "geeksforgeeks" # Printing original string print("The original string is : " + str(test_str)) # Computing half index length = len(test_str) hlf_idx = length // 2 # Initializing result string result = "" # Iterating through characters for i in range(length): # Conditional operator result += test_str[i] if i < hlf_idx else test_str[i].upper() # Printing result print("The resultant string : " + str(result)) #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]
Python - Uppercase Half String
https://www.geeksforgeeks.org/python-uppercase-half-string/
from itertools import islice test_str = "geeksforgeeks" # printing original string print("The original string is : " + str(test_str)) hlf_idx = len(test_str) // 2 res = "".join( [c.upper() if i >= hlf_idx else c for i, c in enumerate(islice(test_str, None))] ) # printing result print("The resultant string : " + str(res)) # This code is contributed by Jyothi pinjala.
#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE
Python - Uppercase Half String from itertools import islice test_str = "geeksforgeeks" # printing original string print("The original string is : " + str(test_str)) hlf_idx = len(test_str) // 2 res = "".join( [c.upper() if i >= hlf_idx else c for i, c in enumerate(islice(test_str, None))] ) # printing result print("The resultant string : " + str(res)) # This code is contributed by Jyothi pinjala. #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]
Python - Uppercase Half String
https://www.geeksforgeeks.org/python-uppercase-half-string/
test_str = "geeksforgeeks" # printing original string print("The original string is : " + str(test_str)) hlf_idx = len(test_str) // 2 res = "".join(map(lambda c: c.upper() if test_str.index(c) >= hlf_idx else c, test_str)) # printing result print("The resultant string : " + str(res))
#Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE
Python - Uppercase Half String test_str = "geeksforgeeks" # printing original string print("The original string is : " + str(test_str)) hlf_idx = len(test_str) // 2 res = "".join(map(lambda c: c.upper() if test_str.index(c) >= hlf_idx else c, test_str)) # printing result print("The resultant string : " + str(res)) #Input : test_str = 'geeksforgeek'?????? #Output : geeksfORGE [END]
Python program to capitalize the first and last character of each word in a string
https://www.geeksforgeeks.org/python-program-to-capitalize-the-first-and-last-character-of-each-word-in-a-string/
# Python program to capitalize # first and last character of # each word of a String # Function to do the same def word_both_cap(str): # lambda function for capitalizing the # first and last letter of words in # the string return " ".join(map(lambda s: s[:-1] + s[-1].upper(), s.title().split())) # Driver's code s = "welcome to geeksforgeeks" print("String before:", s) print("String after:", word_both_cap(str))
Input: hello world Output: HellO WorlD
Python program to capitalize the first and last character of each word in a string # Python program to capitalize # first and last character of # each word of a String # Function to do the same def word_both_cap(str): # lambda function for capitalizing the # first and last letter of words in # the string return " ".join(map(lambda s: s[:-1] + s[-1].upper(), s.title().split())) # Driver's code s = "welcome to geeksforgeeks" print("String before:", s) print("String after:", word_both_cap(str)) Input: hello world Output: HellO WorlD [END]
Python program to capitalize the first and last character of each word in a string
https://www.geeksforgeeks.org/python-program-to-capitalize-the-first-and-last-character-of-each-word-in-a-string/
# Python program to capitalize # first and last character of # each word of a String s = "welcome to geeksforgeeks" print("String before:", s) a = s.split() res = [] for i in a: x = i[0].upper() + i[1:-1] + i[-1].upper() res.append(x) res = " ".join(res) print("String after:", res)
Input: hello world Output: HellO WorlD
Python program to capitalize the first and last character of each word in a string # Python program to capitalize # first and last character of # each word of a String s = "welcome to geeksforgeeks" print("String before:", s) a = s.split() res = [] for i in a: x = i[0].upper() + i[1:-1] + i[-1].upper() res.append(x) res = " ".join(res) print("String after:", res) Input: hello world Output: HellO WorlD [END]
Python program to check if a string has at least one letter and one number
https://www.geeksforgeeks.org/python-program-to-check-if-a-string-has-at-least-one-letter-and-one-number/
def checkString(str): # initializing flag variable flag_l = False flag_n = False # checking for letter and numbers in # given string for i in str: # if string has letter if i.isalpha(): flag_l = True # if string has number if i.isdigit(): flag_n = True # returning and of flag # for checking required condition return flag_l and flag_n # driver code print(checkString("thishasboth29")) print(checkString("geeksforgeeks"))
Input: welcome2ourcountry34 Output: True
Python program to check if a string has at least one letter and one number def checkString(str): # initializing flag variable flag_l = False flag_n = False # checking for letter and numbers in # given string for i in str: # if string has letter if i.isalpha(): flag_l = True # if string has number if i.isdigit(): flag_n = True # returning and of flag # for checking required condition return flag_l and flag_n # driver code print(checkString("thishasboth29")) print(checkString("geeksforgeeks")) Input: welcome2ourcountry34 Output: True [END]
Python program to check if a string has at least one letter and one number
https://www.geeksforgeeks.org/python-program-to-check-if-a-string-has-at-least-one-letter-and-one-number/
def checkString(str): # initializing flag variable flag_l = False flag_n = False # checking for letter and numbers in # given string for i in str: # if string has letter if i in "abcdefghijklmnopqrstuvwxyz": flag_l = True # if string has number if i in "0123456789": flag_n = True # returning and of flag # for checking required condition return flag_l and flag_n # driver code print(checkString("thishasboth29")) print(checkString("geeksforgeeks"))
Input: welcome2ourcountry34 Output: True
Python program to check if a string has at least one letter and one number def checkString(str): # initializing flag variable flag_l = False flag_n = False # checking for letter and numbers in # given string for i in str: # if string has letter if i in "abcdefghijklmnopqrstuvwxyz": flag_l = True # if string has number if i in "0123456789": flag_n = True # returning and of flag # for checking required condition return flag_l and flag_n # driver code print(checkString("thishasboth29")) print(checkString("geeksforgeeks")) Input: welcome2ourcountry34 Output: True [END]
Python program to check if a string has at least one letter and one number
https://www.geeksforgeeks.org/python-program-to-check-if-a-string-has-at-least-one-letter-and-one-number/
import operator as op def checkString(str): letters = "abcdefghijklmnopqrstuvwxyz" digits = "0123456789" # initializing flag variable flag_l = False flag_n = False # checking for letter and numbers in # given string for i in str: # if string has letter if op.countOf(letters, i) > 0: flag_l = True # if string has digits if op.countOf(digits, i) > 0: flag_n = True # returning and of flag # for checking required condition return flag_l and flag_n # driver code print(checkString("thishasboth29")) print(checkString("geeksforgeeks"))
Input: welcome2ourcountry34 Output: True
Python program to check if a string has at least one letter and one number import operator as op def checkString(str): letters = "abcdefghijklmnopqrstuvwxyz" digits = "0123456789" # initializing flag variable flag_l = False flag_n = False # checking for letter and numbers in # given string for i in str: # if string has letter if op.countOf(letters, i) > 0: flag_l = True # if string has digits if op.countOf(digits, i) > 0: flag_n = True # returning and of flag # for checking required condition return flag_l and flag_n # driver code print(checkString("thishasboth29")) print(checkString("geeksforgeeks")) Input: welcome2ourcountry34 Output: True [END]
Python program to check if a string has at least one letter and one number
https://www.geeksforgeeks.org/python-program-to-check-if-a-string-has-at-least-one-letter-and-one-number/
import re def checkString(str): # using regular expression to check if a string contains # at least one letter and one number match = re.search(r"[a-zA-Z]+", str) and re.search(r"[0-9]+", str) if match: return True else: return False # driver code print(checkString("thishasboth29")) print(checkString("geeksforgeeks")) # This code is contributed by Vinay Pinjala.
Input: welcome2ourcountry34 Output: True
Python program to check if a string has at least one letter and one number import re def checkString(str): # using regular expression to check if a string contains # at least one letter and one number match = re.search(r"[a-zA-Z]+", str) and re.search(r"[0-9]+", str) if match: return True else: return False # driver code print(checkString("thishasboth29")) print(checkString("geeksforgeeks")) # This code is contributed by Vinay Pinjala. Input: welcome2ourcountry34 Output: True [END]
Python program to check if a string has at least one letter and one number
https://www.geeksforgeeks.org/python-program-to-check-if-a-string-has-at-least-one-letter-and-one-number/
def checkString(str): # Create sets of letters and digits letters = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") digits = set("0123456789") # Check if the intersection of the input string and the sets of letters and digits is not empty return bool(letters & set(str)) and bool(digits & set(str)) # Test the function with two sample inputs print(checkString("thishasboth29")) print(checkString("geeksforgeeks")) # This code is contributed by Jyothi pinjala.
Input: welcome2ourcountry34 Output: True
Python program to check if a string has at least one letter and one number def checkString(str): # Create sets of letters and digits letters = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") digits = set("0123456789") # Check if the intersection of the input string and the sets of letters and digits is not empty return bool(letters & set(str)) and bool(digits & set(str)) # Test the function with two sample inputs print(checkString("thishasboth29")) print(checkString("geeksforgeeks")) # This code is contributed by Jyothi pinjala. Input: welcome2ourcountry34 Output: True [END]
Python program to check if a string has at least one letter and one number
https://www.geeksforgeeks.org/python-program-to-check-if-a-string-has-at-least-one-letter-and-one-number/
checkString = lambda s: any(c.isalpha() for c in s) and any(c.isdigit() for c in s) # driver code print(checkString("thishasboth29")) print(checkString("geeksforgeeks"))
Input: welcome2ourcountry34 Output: True
Python program to check if a string has at least one letter and one number checkString = lambda s: any(c.isalpha() for c in s) and any(c.isdigit() for c in s) # driver code print(checkString("thishasboth29")) print(checkString("geeksforgeeks")) Input: welcome2ourcountry34 Output: True [END]
Python | Program to accept the strings which contains all vowelsvowels
https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/
# Python program to accept the strings # which contains all the vowels # Function for check if string # is accepted or not def check(string): string = string.lower() # set() function convert "aeiou" # string into set of characters # i.e.vowels = {'a', 'e', 'i', 'o', 'u'} vowels = set("aeiou") # set() function convert empty # dictionary into empty set s = set({}) # looping through each # character of the string for char in string: # Check for the character is present inside # the vowels set or not. If present, then # add into the set s by using add method if char in vowels: s.add(char) else: pass # check the length of set s equal to length # of vowels set or not. If equal, string is # accepted otherwise not if len(s) == len(vowels): print("Accepted") else: print("Not Accepted") # Driver code if __name__ == "__main__": string = "SEEquoiaL" # calling function check(string)
#Input : geeksforgeeks #Output : Not Accepted
Python | Program to accept the strings which contains all vowelsvowels # Python program to accept the strings # which contains all the vowels # Function for check if string # is accepted or not def check(string): string = string.lower() # set() function convert "aeiou" # string into set of characters # i.e.vowels = {'a', 'e', 'i', 'o', 'u'} vowels = set("aeiou") # set() function convert empty # dictionary into empty set s = set({}) # looping through each # character of the string for char in string: # Check for the character is present inside # the vowels set or not. If present, then # add into the set s by using add method if char in vowels: s.add(char) else: pass # check the length of set s equal to length # of vowels set or not. If equal, string is # accepted otherwise not if len(s) == len(vowels): print("Accepted") else: print("Not Accepted") # Driver code if __name__ == "__main__": string = "SEEquoiaL" # calling function check(string) #Input : geeksforgeeks #Output : Not Accepted [END]
Python | Program to accept the strings which contains all vowelsvowels
https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/
def check(string): string = string.replace(" ", "") string = string.lower() vowel = [ string.count("a"), string.count("e"), string.count("i"), string.count("o"), string.count("u"), ] # If 0 is present int vowel count array if vowel.count(0) > 0: return "not accepted" else: return "accepted" # Driver code if __name__ == "__main__": string = "SEEquoiaL" print(check(string))
#Input : geeksforgeeks #Output : Not Accepted
Python | Program to accept the strings which contains all vowelsvowels def check(string): string = string.replace(" ", "") string = string.lower() vowel = [ string.count("a"), string.count("e"), string.count("i"), string.count("o"), string.count("u"), ] # If 0 is present int vowel count array if vowel.count(0) > 0: return "not accepted" else: return "accepted" # Driver code if __name__ == "__main__": string = "SEEquoiaL" print(check(string)) #Input : geeksforgeeks #Output : Not Accepted [END]
Python | Program to accept the strings which contains all vowelsvowels
https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/
# Python program for the above approach def check(string): if len(set(string.lower()).intersection("aeiou")) >= 5: return "accepted" else: return "not accepted" # Driver code if __name__ == "__main__": string = "geeksforgeeks" print(check(string))
#Input : geeksforgeeks #Output : Not Accepted
Python | Program to accept the strings which contains all vowelsvowels # Python program for the above approach def check(string): if len(set(string.lower()).intersection("aeiou")) >= 5: return "accepted" else: return "not accepted" # Driver code if __name__ == "__main__": string = "geeksforgeeks" print(check(string)) #Input : geeksforgeeks #Output : Not Accepted [END]
Python | Program to accept the strings which contains all vowelsvowels
https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/
# import library import re sampleInput = "aeioAEiuioea" # regular expression to find the strings # which have characters other than a,e,i,o and u c = re.compile("[^aeiouAEIOU]") # use findall() to get the list of strings # that have characters other than a,e,i,o and u. if len(c.findall(sampleInput)): print("Not Accepted") # if length of list > 0 then it is not accepted else: print("Accepted") # if length of list = 0 then it is accepted
#Input : geeksforgeeks #Output : Not Accepted
Python | Program to accept the strings which contains all vowelsvowels # import library import re sampleInput = "aeioAEiuioea" # regular expression to find the strings # which have characters other than a,e,i,o and u c = re.compile("[^aeiouAEIOU]") # use findall() to get the list of strings # that have characters other than a,e,i,o and u. if len(c.findall(sampleInput)): print("Not Accepted") # if length of list > 0 then it is not accepted else: print("Accepted") # if length of list = 0 then it is accepted #Input : geeksforgeeks #Output : Not Accepted [END]
Python | Program to accept the strings which contains all vowelsvowels
https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/
# Python | Program to accept the strings which contains all vowels def all_vowels(str_value): new_list = [char for char in str_value.lower() if char in "aeiou"] if new_list: dic, lst = {}, [] for char in new_list: dic["a"] = new_list.count("a") dic["e"] = new_list.count("e") dic["i"] = new_list.count("i") dic["o"] = new_list.count("o") dic["u"] = new_list.count("u") for i, j in dic.items(): if j == 0: lst.append(i) if lst: return f"All vowels except {','.join(lst)} are not present" else: return "All vowels are present" else: return "No vowels present" # function-call str_value = "geeksforgeeks" print(all_vowels(str_value)) str_value = "ABeeIghiObhkUul" print(all_vowels(str_value)) # contribute by saikot
#Input : geeksforgeeks #Output : Not Accepted
Python | Program to accept the strings which contains all vowelsvowels # Python | Program to accept the strings which contains all vowels def all_vowels(str_value): new_list = [char for char in str_value.lower() if char in "aeiou"] if new_list: dic, lst = {}, [] for char in new_list: dic["a"] = new_list.count("a") dic["e"] = new_list.count("e") dic["i"] = new_list.count("i") dic["o"] = new_list.count("o") dic["u"] = new_list.count("u") for i, j in dic.items(): if j == 0: lst.append(i) if lst: return f"All vowels except {','.join(lst)} are not present" else: return "All vowels are present" else: return "No vowels present" # function-call str_value = "geeksforgeeks" print(all_vowels(str_value)) str_value = "ABeeIghiObhkUul" print(all_vowels(str_value)) # contribute by saikot #Input : geeksforgeeks #Output : Not Accepted [END]
Python | Program to accept the strings which contains all vowelsvowels
https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/
# Python program to accept the strings # which contains all the vowels # Function for check if string # is accepted or not def check(string): # Checking if "aeiou" is a subset of the set of all letters in the string if set("aeiou").issubset(set(string.lower())): return "Accepted" return "Not accepted" # Driver code if __name__ == "__main__": string = "SEEquoiaL" # calling function print(check(string))
#Input : geeksforgeeks #Output : Not Accepted
Python | Program to accept the strings which contains all vowelsvowels # Python program to accept the strings # which contains all the vowels # Function for check if string # is accepted or not def check(string): # Checking if "aeiou" is a subset of the set of all letters in the string if set("aeiou").issubset(set(string.lower())): return "Accepted" return "Not accepted" # Driver code if __name__ == "__main__": string = "SEEquoiaL" # calling function print(check(string)) #Input : geeksforgeeks #Output : Not Accepted [END]
Python | Program to accept the strings which contains all vowelsvowels
https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/
import collections def check(string): # create a Counter object to count the occurrences of each character counter = collections.Counter(string.lower()) # set of vowels vowels = set("aeiou") # counter for the number of vowels present vowel_count = 0 # check if each vowel is present in the string for vowel in vowels: if counter[vowel] > 0: vowel_count += 1 # check if all vowels are present if vowel_count == len(vowels): print("Accepted") else: print("Not Accepted") # test the function string = "SEEquoiaL" check(string)
#Input : geeksforgeeks #Output : Not Accepted
Python | Program to accept the strings which contains all vowelsvowels import collections def check(string): # create a Counter object to count the occurrences of each character counter = collections.Counter(string.lower()) # set of vowels vowels = set("aeiou") # counter for the number of vowels present vowel_count = 0 # check if each vowel is present in the string for vowel in vowels: if counter[vowel] > 0: vowel_count += 1 # check if all vowels are present if vowel_count == len(vowels): print("Accepted") else: print("Not Accepted") # test the function string = "SEEquoiaL" check(string) #Input : geeksforgeeks #Output : Not Accepted [END]
Python | Program to accept the strings which contains all vowelsvowels
https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/
# Python program to accept the strings # which contains all the vowels # Function for check if string # is accepted or not # using all() method def check(string): vowels = "aeiou" # storing vowels if all(vowel in string.lower() for vowel in vowels): return "Accepted" return "Not accepted" # initializing string string = "SEEquoiaL" # test the function print(check(string)) # this code contributed by tvsk
#Input : geeksforgeeks #Output : Not Accepted
Python | Program to accept the strings which contains all vowelsvowels # Python program to accept the strings # which contains all the vowels # Function for check if string # is accepted or not # using all() method def check(string): vowels = "aeiou" # storing vowels if all(vowel in string.lower() for vowel in vowels): return "Accepted" return "Not accepted" # initializing string string = "SEEquoiaL" # test the function print(check(string)) # this code contributed by tvsk #Input : geeksforgeeks #Output : Not Accepted [END]
Python | Program to accept the strings which contains all vowelsvowels
https://www.geeksforgeeks.org/python-program-to-accept-the-strings-which-contains-all-vowels/
# function definition def check(s): A = {"a", "e", "i", "o", "u"} # using the set difference if len(A.difference(set(s.lower()))) == 0: print("accepted") else: print("not accepted") # input s = "SEEquoiaL" # function call check(s)
#Input : geeksforgeeks #Output : Not Accepted
Python | Program to accept the strings which contains all vowelsvowels # function definition def check(s): A = {"a", "e", "i", "o", "u"} # using the set difference if len(A.difference(set(s.lower()))) == 0: print("accepted") else: print("not accepted") # input s = "SEEquoiaL" # function call check(s) #Input : geeksforgeeks #Output : Not Accepted [END]
Python | Count the Number of matching characters in a pair of string
https://www.geeksforgeeks.org/python-count-the-number-of-matching-characters-in-a-pair-of-string/
// C++ code to count number of matching// characters in a pair of strings??????# include <bits/stdc++.h>using namespace std??????// Function to count the matching charactersvoid count(string str1, string str2){????????????????????????int c = 0, j = 0??????????????????????????????// Traverse the string 1 char by char????????????????????????for (int i=0??????????????????????????????????????????????????????i < str1.length()??????????????????????????????????????????????????????i++) {??????????????????????????????????????????????????????// This will check if str1[i]????????????????????????????????????????????????// is present in str2 or not????????????????????????????????????????????????// str2.find(str1[i]) returns - 1 if not found?????????????????????????????????????????????"No. of matching characters are: "????????<< c / 2}??// Driver codeint main(){????????string str1 = "aabcddekll12@"????????string str2 = "bb2211@55k"??????????count(str1, str2)}
#Input : str1 = 'abcdef' str2 = 'defghia'
Python | Count the Number of matching characters in a pair of string // C++ code to count number of matching// characters in a pair of strings??????# include <bits/stdc++.h>using namespace std??????// Function to count the matching charactersvoid count(string str1, string str2){????????????????????????int c = 0, j = 0??????????????????????????????// Traverse the string 1 char by char????????????????????????for (int i=0??????????????????????????????????????????????????????i < str1.length()??????????????????????????????????????????????????????i++) {??????????????????????????????????????????????????????// This will check if str1[i]????????????????????????????????????????????????// is present in str2 or not????????????????????????????????????????????????// str2.find(str1[i]) returns - 1 if not found?????????????????????????????????????????????"No. of matching characters are: "????????<< c / 2}??// Driver codeint main(){????????string str1 = "aabcddekll12@"????????string str2 = "bb2211@55k"??????????count(str1, str2)} #Input : str1 = 'abcdef' str2 = 'defghia' [END]
Python | Count the Number of matching characters in a pair of string
https://www.geeksforgeeks.org/python-count-the-number-of-matching-characters-in-a-pair-of-string/
# Python code to count number of unique matching # characters in a pair of strings # count function count the common unique # characters present in both strings . def count(str1, str2): # set of characters of string1 set_string1 = set(str1) # set of characters of string2 set_string2 = set(str2) # using (&) intersection mathematical operation on sets # the unique characters present in both the strings # are stored in matched_characters set variable matched_characters = set_string1 & set_string2 # printing the length of matched_characters set # gives the no. of matched characters print("No. of matching characters are : " + str(len(matched_characters))) # Driver code if __name__ == "__main__": str1 = "aabcddekll12@" # first string str2 = "bb2211@55k" # second string # call count function count(str1, str2)
#Input : str1 = 'abcdef' str2 = 'defghia'
Python | Count the Number of matching characters in a pair of string # Python code to count number of unique matching # characters in a pair of strings # count function count the common unique # characters present in both strings . def count(str1, str2): # set of characters of string1 set_string1 = set(str1) # set of characters of string2 set_string2 = set(str2) # using (&) intersection mathematical operation on sets # the unique characters present in both the strings # are stored in matched_characters set variable matched_characters = set_string1 & set_string2 # printing the length of matched_characters set # gives the no. of matched characters print("No. of matching characters are : " + str(len(matched_characters))) # Driver code if __name__ == "__main__": str1 = "aabcddekll12@" # first string str2 = "bb2211@55k" # second string # call count function count(str1, str2) #Input : str1 = 'abcdef' str2 = 'defghia' [END]
Python | Count the Number of matching characters in a pair of string
https://www.geeksforgeeks.org/python-count-the-number-of-matching-characters-in-a-pair-of-string/
def count(str1, str2): # Initialize an empty dictionary to keep track of the count of each character in str1. char_count = {} # Iterate over each character in str1. for char in str1: # If the character is already in the dictionary, increment its count. if char in char_count: char_count[char] += 1 # Otherwise, add the character to the dictionary with a count of 1. else: char_count[char] = 1 # Initialize a counter variable to 0. counter = 0 # Iterate over each character in str2. for char in str2: # If the character is in the char_count dictionary and its count is greater than 0, increment the counter and decrement the count. if char in char_count and char_count[char] > 0: counter += 1 char_count[char] -= 1 # Print the number of matching characters. print("No. of matching characters are: " + str(counter)) # Driver code if __name__ == "__main__": # Define two strings to compare. str1 = "aabcddekll12@" str2 = "bb2211@55k" # Call the count function with the two strings. count(str1, str2)
#Input : str1 = 'abcdef' str2 = 'defghia'
Python | Count the Number of matching characters in a pair of string def count(str1, str2): # Initialize an empty dictionary to keep track of the count of each character in str1. char_count = {} # Iterate over each character in str1. for char in str1: # If the character is already in the dictionary, increment its count. if char in char_count: char_count[char] += 1 # Otherwise, add the character to the dictionary with a count of 1. else: char_count[char] = 1 # Initialize a counter variable to 0. counter = 0 # Iterate over each character in str2. for char in str2: # If the character is in the char_count dictionary and its count is greater than 0, increment the counter and decrement the count. if char in char_count and char_count[char] > 0: counter += 1 char_count[char] -= 1 # Print the number of matching characters. print("No. of matching characters are: " + str(counter)) # Driver code if __name__ == "__main__": # Define two strings to compare. str1 = "aabcddekll12@" str2 = "bb2211@55k" # Call the count function with the two strings. count(str1, str2) #Input : str1 = 'abcdef' str2 = 'defghia' [END]
Python program to count number of vowels using sets in given string
https://www.geeksforgeeks.org/python-program-count-number-vowels-using-sets-given-string/
# Python3 code to count vowel in # a string using set # Function to count vowel def vowel_count(str): # Initializing count variable to 0 count = 0 # Creating a set of vowels vowel = set("aeiouAEIOU") # Loop to traverse the alphabet # in the given string for alphabet in str: # If alphabet is present # in set vowel if alphabet in vowel: count = count + 1 print("No. of vowels :", count) # Driver code str = "GeeksforGeeks" # Function Call vowel_count(str)
#Input : GeeksforGeeks #Output : No. of vowels : 5
Python program to count number of vowels using sets in given string # Python3 code to count vowel in # a string using set # Function to count vowel def vowel_count(str): # Initializing count variable to 0 count = 0 # Creating a set of vowels vowel = set("aeiouAEIOU") # Loop to traverse the alphabet # in the given string for alphabet in str: # If alphabet is present # in set vowel if alphabet in vowel: count = count + 1 print("No. of vowels :", count) # Driver code str = "GeeksforGeeks" # Function Call vowel_count(str) #Input : GeeksforGeeks #Output : No. of vowels : 5 [END]
Python program to count number of vowels using sets in given string
https://www.geeksforgeeks.org/python-program-count-number-vowels-using-sets-given-string/
def vowel_count(str): # Creating a list of vowels vowels = "aeiouAEIOU" # Using list comprehension to count the number of vowels in the string count = len([char for char in str if char in vowels]) # Printing the count of vowels in the string print("No. of vowels :", count) # Driver code str = "GeeksforGeeks" # Function Call vowel_count(str)
#Input : GeeksforGeeks #Output : No. of vowels : 5
Python program to count number of vowels using sets in given string def vowel_count(str): # Creating a list of vowels vowels = "aeiouAEIOU" # Using list comprehension to count the number of vowels in the string count = len([char for char in str if char in vowels]) # Printing the count of vowels in the string print("No. of vowels :", count) # Driver code str = "GeeksforGeeks" # Function Call vowel_count(str) #Input : GeeksforGeeks #Output : No. of vowels : 5 [END]
Python Program to remove all duplicates from a given string
https://www.geeksforgeeks.org/remove-duplicates-given-string-python/
from collections import OrderedDict # Function to remove all duplicates from string # and order does not matter def removeDupWithoutOrder(str): # set() --> A Set is an unordered collection # data type that is iterable, mutable, # and has no duplicate elements. # "".join() --> It joins two adjacent elements in # iterable with any symbol defined in # "" ( double quotes ) and returns a # single string return "".join(set(str)) # Function to remove all duplicates from string # and keep the order of characters same def removeDupWithOrder(str): return "".join(OrderedDict.fromkeys(str)) # Driver program if __name__ == "__main__": str = "geeksforgeeks" print("Without Order = ", removeDupWithoutOrder(str)) print("With Order = ", removeDupWithOrder(str))
#Output : Without Order = foskerg
Python Program to remove all duplicates from a given string from collections import OrderedDict # Function to remove all duplicates from string # and order does not matter def removeDupWithoutOrder(str): # set() --> A Set is an unordered collection # data type that is iterable, mutable, # and has no duplicate elements. # "".join() --> It joins two adjacent elements in # iterable with any symbol defined in # "" ( double quotes ) and returns a # single string return "".join(set(str)) # Function to remove all duplicates from string # and keep the order of characters same def removeDupWithOrder(str): return "".join(OrderedDict.fromkeys(str)) # Driver program if __name__ == "__main__": str = "geeksforgeeks" print("Without Order = ", removeDupWithoutOrder(str)) print("With Order = ", removeDupWithOrder(str)) #Output : Without Order = foskerg [END]
Python Program to remove all duplicates from a given string
https://www.geeksforgeeks.org/remove-duplicates-given-string-python/
def removeDuplicate(str): s = set(str) s = "".join(s) print("Without Order:", s) t = "" for i in str: if i in t: pass else: t = t + i print("With Order:", t) str = "geeksforgeeks" removeDuplicate(str)
#Output : Without Order = foskerg
Python Program to remove all duplicates from a given string def removeDuplicate(str): s = set(str) s = "".join(s) print("Without Order:", s) t = "" for i in str: if i in t: pass else: t = t + i print("With Order:", t) str = "geeksforgeeks" removeDuplicate(str) #Output : Without Order = foskerg [END]
Python Program to remove all duplicates from a given string
https://www.geeksforgeeks.org/remove-duplicates-given-string-python/
from collections import OrderedDict ordinary_dictionary = {} ordinary_dictionary["a"] = 1 ordinary_dictionary["b"] = 2 ordinary_dictionary["c"] = 3 ordinary_dictionary["d"] = 4 ordinary_dictionary["e"] = 5 # Output = {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4} print(ordinary_dictionary) ordered_dictionary = OrderedDict() ordered_dictionary["a"] = 1 ordered_dictionary["b"] = 2 ordered_dictionary["c"] = 3 ordered_dictionary["d"] = 4 ordered_dictionary["e"] = 5 # Output = {'a':1,'b':2,'c':3,'d':4,'e':5} print(ordered_dictionary)
#Output : Without Order = foskerg
Python Program to remove all duplicates from a given string from collections import OrderedDict ordinary_dictionary = {} ordinary_dictionary["a"] = 1 ordinary_dictionary["b"] = 2 ordinary_dictionary["c"] = 3 ordinary_dictionary["d"] = 4 ordinary_dictionary["e"] = 5 # Output = {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4} print(ordinary_dictionary) ordered_dictionary = OrderedDict() ordered_dictionary["a"] = 1 ordered_dictionary["b"] = 2 ordered_dictionary["c"] = 3 ordered_dictionary["d"] = 4 ordered_dictionary["e"] = 5 # Output = {'a':1,'b':2,'c':3,'d':4,'e':5} print(ordered_dictionary) #Output : Without Order = foskerg [END]
Python Program to remove all duplicates from a given string
https://www.geeksforgeeks.org/remove-duplicates-given-string-python/
from collections import OrderedDict seq = ("name", "age", "gender") dict = OrderedDict.fromkeys(seq) # Output = {'age': None, 'name': None, 'gender': None} print(str(dict)) dict = OrderedDict.fromkeys(seq, 10) # Output = {'age': 10, 'name': 10, 'gender': 10} print(str(dict))
#Output : Without Order = foskerg
Python Program to remove all duplicates from a given string from collections import OrderedDict seq = ("name", "age", "gender") dict = OrderedDict.fromkeys(seq) # Output = {'age': None, 'name': None, 'gender': None} print(str(dict)) dict = OrderedDict.fromkeys(seq, 10) # Output = {'age': 10, 'name': 10, 'gender': 10} print(str(dict)) #Output : Without Order = foskerg [END]
Python Program to remove all duplicates from a given string
https://www.geeksforgeeks.org/remove-duplicates-given-string-python/
import operator as op def removeDuplicate(str): s = set(str) s = "".join(s) print("Without Order:", s) t = "" for i in str: if op.countOf(t, i) > 0: pass else: t = t + i print("With Order:", t) str = "geeksforgeeks" removeDuplicate(str)
#Output : Without Order = foskerg
Python Program to remove all duplicates from a given string import operator as op def removeDuplicate(str): s = set(str) s = "".join(s) print("Without Order:", s) t = "" for i in str: if op.countOf(t, i) > 0: pass else: t = t + i print("With Order:", t) str = "geeksforgeeks" removeDuplicate(str) #Output : Without Order = foskerg [END]
Python - Least Frequent Character in String
https://www.geeksforgeeks.org/python-least-frequent-character-in-string/?ref=leftbar-rightbar
# Python 3 code to demonstrate # Least Frequent Character in String # naive method # initializing string test_str = "GeeksforGeeks" # printing original string print("The original string is : " + test_str) # using naive method to get # Least Frequent Character in String all_freq = {} for i in test_str: if i in all_freq: all_freq[i] += 1 else: all_freq[i] = 1 res = min(all_freq, key=all_freq.get) # printing result print("The minimum of all characters in GeeksforGeeks is : " + str(res))
#Output : The original string is : GeeksforGeeks
Python - Least Frequent Character in String # Python 3 code to demonstrate # Least Frequent Character in String # naive method # initializing string test_str = "GeeksforGeeks" # printing original string print("The original string is : " + test_str) # using naive method to get # Least Frequent Character in String all_freq = {} for i in test_str: if i in all_freq: all_freq[i] += 1 else: all_freq[i] = 1 res = min(all_freq, key=all_freq.get) # printing result print("The minimum of all characters in GeeksforGeeks is : " + str(res)) #Output : The original string is : GeeksforGeeks [END]
Python - Least Frequent Character in String
https://www.geeksforgeeks.org/python-least-frequent-character-in-string/?ref=leftbar-rightbar
# Python 3 code to demonstrate # Least Frequent Character in String # collections.Counter() + min() from collections import Counter # initializing string test_str = "GeeksforGeeks" # printing original string print("The original string is : " + test_str) # using collections.Counter() + min() to get # Least Frequent Character in String res = Counter(test_str) res = min(res, key=res.get) # printing result print("The minimum of all characters in GeeksforGeeks is : " + str(res))
#Output : The original string is : GeeksforGeeks
Python - Least Frequent Character in String # Python 3 code to demonstrate # Least Frequent Character in String # collections.Counter() + min() from collections import Counter # initializing string test_str = "GeeksforGeeks" # printing original string print("The original string is : " + test_str) # using collections.Counter() + min() to get # Least Frequent Character in String res = Counter(test_str) res = min(res, key=res.get) # printing result print("The minimum of all characters in GeeksforGeeks is : " + str(res)) #Output : The original string is : GeeksforGeeks [END]
Python - Least Frequent Character in String
https://www.geeksforgeeks.org/python-least-frequent-character-in-string/?ref=leftbar-rightbar
from collections import defaultdict def least_frequent_char(string): freq = defaultdict(int) for char in string: freq[char] += 1 min_freq = min(freq.values()) least_frequent_chars = [char for char in freq if freq[char] == min_freq] return "".join(sorted(least_frequent_chars))[0] # Example usage test_str = "GeeksorfGeeks" least_frequent = least_frequent_char(test_str) print(f"The least frequent character in '{test_str}' is: {least_frequent}")
#Output : The original string is : GeeksforGeeks
Python - Least Frequent Character in String from collections import defaultdict def least_frequent_char(string): freq = defaultdict(int) for char in string: freq[char] += 1 min_freq = min(freq.values()) least_frequent_chars = [char for char in freq if freq[char] == min_freq] return "".join(sorted(least_frequent_chars))[0] # Example usage test_str = "GeeksorfGeeks" least_frequent = least_frequent_char(test_str) print(f"The least frequent character in '{test_str}' is: {least_frequent}") #Output : The original string is : GeeksforGeeks [END]
Python - Least Frequent Character in String
https://www.geeksforgeeks.org/python-least-frequent-character-in-string/?ref=leftbar-rightbar
import numpy as np def least_frequent_char(string): freq = {char: string.count(char) for char in set(string)} return list(freq.keys())[np.argmin(list(freq.values()))] # Example usage input_string = "GeeksforGeeks" min_char = least_frequent_char(input_string) print("The original string is:", input_string) print("The minimum of all characters in", input_string, "is:", min_char) # This code is contributed by Jyothi Pinjala.
#Output : The original string is : GeeksforGeeks
Python - Least Frequent Character in String import numpy as np def least_frequent_char(string): freq = {char: string.count(char) for char in set(string)} return list(freq.keys())[np.argmin(list(freq.values()))] # Example usage input_string = "GeeksforGeeks" min_char = least_frequent_char(input_string) print("The original string is:", input_string) print("The minimum of all characters in", input_string, "is:", min_char) # This code is contributed by Jyothi Pinjala. #Output : The original string is : GeeksforGeeks [END]
Python - Least Frequent Character in String
https://www.geeksforgeeks.org/python-least-frequent-character-in-string/?ref=leftbar-rightbar
# Python program for the above approach # Function to find the least frequent characters def least_frequent_char(input_str): freq_dict = {} for char in input_str: freq_dict[char] = freq_dict.get(char, 0) + 1 min_value = min(freq_dict.values()) least_frequent_char = "" # Traversing the dictionary for key, value in freq_dict.items(): if value == min_value: least_frequent_char = key break return least_frequent_char # Driver Code input_str = "geeksforgeeks" print(least_frequent_char(input_str))
#Output : The original string is : GeeksforGeeks
Python - Least Frequent Character in String # Python program for the above approach # Function to find the least frequent characters def least_frequent_char(input_str): freq_dict = {} for char in input_str: freq_dict[char] = freq_dict.get(char, 0) + 1 min_value = min(freq_dict.values()) least_frequent_char = "" # Traversing the dictionary for key, value in freq_dict.items(): if value == min_value: least_frequent_char = key break return least_frequent_char # Driver Code input_str = "geeksforgeeks" print(least_frequent_char(input_str)) #Output : The original string is : GeeksforGeeks [END]
Python | Maximum frequency character in String
https://www.geeksforgeeks.org/python-maximum-frequency-character-in-string/?ref=leftbar-rightbar
# Python 3 code to demonstrate # Maximum frequency character in String # naive method # initializing string test_str = "GeeksforGeeks" # printing original string print("The original string is : " + test_str) # using naive method to get # Maximum frequency character in String all_freq = {} for i in test_str: if i in all_freq: all_freq[i] += 1 else: all_freq[i] = 1 res = max(all_freq, key=all_freq.get) # printing result print("The maximum of all characters in GeeksforGeeks is : " + str(res))
#Output : The original string is : GeeksforGeeks
Python | Maximum frequency character in String # Python 3 code to demonstrate # Maximum frequency character in String # naive method # initializing string test_str = "GeeksforGeeks" # printing original string print("The original string is : " + test_str) # using naive method to get # Maximum frequency character in String all_freq = {} for i in test_str: if i in all_freq: all_freq[i] += 1 else: all_freq[i] = 1 res = max(all_freq, key=all_freq.get) # printing result print("The maximum of all characters in GeeksforGeeks is : " + str(res)) #Output : The original string is : GeeksforGeeks [END]
Python | Maximum frequency character in String
https://www.geeksforgeeks.org/python-maximum-frequency-character-in-string/?ref=leftbar-rightbar
# Python 3 code to demonstrate # Maximum frequency character in String # collections.Counter() + max() from collections import Counter # initializing string test_str = "GeeksforGeeks" # printing original string print("The original string is : " + test_str) # using collections.Counter() + max() to get # Maximum frequency character in String res = Counter(test_str) res = max(res, key=res.get) # printing result print("The maximum of all characters in GeeksforGeeks is : " + str(res))
#Output : The original string is : GeeksforGeeks
Python | Maximum frequency character in String # Python 3 code to demonstrate # Maximum frequency character in String # collections.Counter() + max() from collections import Counter # initializing string test_str = "GeeksforGeeks" # printing original string print("The original string is : " + test_str) # using collections.Counter() + max() to get # Maximum frequency character in String res = Counter(test_str) res = max(res, key=res.get) # printing result print("The maximum of all characters in GeeksforGeeks is : " + str(res)) #Output : The original string is : GeeksforGeeks [END]
Python | Maximum frequency character in String
https://www.geeksforgeeks.org/python-maximum-frequency-character-in-string/?ref=leftbar-rightbar
# initializing string test_str = "GeeksforGeeks" # printing original string print("The original string is : " + test_str) # creating an empty dictionary freq = {} # counting frequency of each character for char in test_str: if char in freq: freq[char] += 1 else: freq[char] = 1 # finding the character with maximum frequency max_char = max(freq, key=freq.get) # printing result print("The maximum of all characters in GeeksforGeeks is : " + str(max_char))
#Output : The original string is : GeeksforGeeks
Python | Maximum frequency character in String # initializing string test_str = "GeeksforGeeks" # printing original string print("The original string is : " + test_str) # creating an empty dictionary freq = {} # counting frequency of each character for char in test_str: if char in freq: freq[char] += 1 else: freq[char] = 1 # finding the character with maximum frequency max_char = max(freq, key=freq.get) # printing result print("The maximum of all characters in GeeksforGeeks is : " + str(max_char)) #Output : The original string is : GeeksforGeeks [END]
Python | Maximum frequency character in String
https://www.geeksforgeeks.org/python-maximum-frequency-character-in-string/?ref=leftbar-rightbar
test_str = "GeeksforGeeks" res = max(test_str, key=lambda x: test_str.count(x)) print(res)
#Output : The original string is : GeeksforGeeks
Python | Maximum frequency character in String test_str = "GeeksforGeeks" res = max(test_str, key=lambda x: test_str.count(x)) print(res) #Output : The original string is : GeeksforGeeks [END]
Python - Odd Frequency Character
https://www.geeksforgeeks.org/python-odd-frequency-characters/?ref=leftbar-rightbar
# Python3 code to demonstrate working of # Odd Frequency Characters # Using list comprehension + defaultdict() from collections import defaultdict # helper_function def hlper_fnc(test_str): cntr = defaultdict(int) for ele in test_str: cntr[ele] += 1 return [val for val, chr in cntr.items() if chr % 2 != 0] # initializing string test_str = "geekforgeeks is best for geeks" # printing original string print("The original string is : " + str(test_str)) # Odd Frequency Characters # Using list comprehension + defaultdict() res = hlper_fnc(test_str) # printing result print("The Odd Frequency Characters are :" + str(res))
#Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's
Python - Odd Frequency Character # Python3 code to demonstrate working of # Odd Frequency Characters # Using list comprehension + defaultdict() from collections import defaultdict # helper_function def hlper_fnc(test_str): cntr = defaultdict(int) for ele in test_str: cntr[ele] += 1 return [val for val, chr in cntr.items() if chr % 2 != 0] # initializing string test_str = "geekforgeeks is best for geeks" # printing original string print("The original string is : " + str(test_str)) # Odd Frequency Characters # Using list comprehension + defaultdict() res = hlper_fnc(test_str) # printing result print("The Odd Frequency Characters are :" + str(res)) #Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's [END]
Python - Odd Frequency Character
https://www.geeksforgeeks.org/python-odd-frequency-characters/?ref=leftbar-rightbar
# Python3 code to demonstrate working of # Odd Frequency Characters # Using list comprehension + Counter() from collections import Counter # initializing string test_str = "geekforgeeks is best for geeks" # printing original string print("The original string is : " + str(test_str)) # Odd Frequency Characters # Using list comprehension + Counter() res = [chr for chr, count in Counter(test_str).items() if count & 1] # printing result print("The Odd Frequency Characters are : " + str(res))
#Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's
Python - Odd Frequency Character # Python3 code to demonstrate working of # Odd Frequency Characters # Using list comprehension + Counter() from collections import Counter # initializing string test_str = "geekforgeeks is best for geeks" # printing original string print("The original string is : " + str(test_str)) # Odd Frequency Characters # Using list comprehension + Counter() res = [chr for chr, count in Counter(test_str).items() if count & 1] # printing result print("The Odd Frequency Characters are : " + str(res)) #Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's [END]
Python - Odd Frequency Character
https://www.geeksforgeeks.org/python-odd-frequency-characters/?ref=leftbar-rightbar
# Python3 code to demonstrate working of # Odd Frequency Characters # initializing string test_str = "geekforgeeks is best for geeks" # printing original string print("The original string is : " + str(test_str)) # Odd Frequency Characters x = set(test_str) res = [] for i in x: if test_str.count(i) % 2 != 0: res.append(i) # printing result print("The Odd Frequency Characters are : " + str(res))
#Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's
Python - Odd Frequency Character # Python3 code to demonstrate working of # Odd Frequency Characters # initializing string test_str = "geekforgeeks is best for geeks" # printing original string print("The original string is : " + str(test_str)) # Odd Frequency Characters x = set(test_str) res = [] for i in x: if test_str.count(i) % 2 != 0: res.append(i) # printing result print("The Odd Frequency Characters are : " + str(res)) #Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's [END]
Python - Odd Frequency Character
https://www.geeksforgeeks.org/python-odd-frequency-characters/?ref=leftbar-rightbar
# Python3 code to demonstrate working of # Odd Frequency Characters import operator as op # initializing string test_str = "geekforgeeks is best for geeks" # printing original string print("The original string is : " + str(test_str)) # Odd Frequency Characters x = set(test_str) res = [] for i in x: if op.countOf(test_str, i) % 2 != 0: res.append(i) # printing result print("The Odd Frequency Characters are : " + str(res))
#Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's
Python - Odd Frequency Character # Python3 code to demonstrate working of # Odd Frequency Characters import operator as op # initializing string test_str = "geekforgeeks is best for geeks" # printing original string print("The original string is : " + str(test_str)) # Odd Frequency Characters x = set(test_str) res = [] for i in x: if op.countOf(test_str, i) % 2 != 0: res.append(i) # printing result print("The Odd Frequency Characters are : " + str(res)) #Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's [END]
Python - Odd Frequency Character
https://www.geeksforgeeks.org/python-odd-frequency-characters/?ref=leftbar-rightbar
def odd_freq_chars(test_str): # Create an empty dictionary to hold character counts char_counts = {} # Loop through each character in the input string for char in test_str: # Increment the count for this character in the dictionary # using the get() method to safely handle uninitialized keys char_counts[char] = char_counts.get(char, 0) + 1 # Create a list of characters whose counts are odd return [char for char, count in char_counts.items() if count % 2 != 0] # Test the function with sample input test_str = "geekforgeeks is best for geeks" print("The original string is : " + str(test_str)) res = odd_freq_chars(test_str) print("The Odd Frequency Characters are :" + str(res))
#Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's
Python - Odd Frequency Character def odd_freq_chars(test_str): # Create an empty dictionary to hold character counts char_counts = {} # Loop through each character in the input string for char in test_str: # Increment the count for this character in the dictionary # using the get() method to safely handle uninitialized keys char_counts[char] = char_counts.get(char, 0) + 1 # Create a list of characters whose counts are odd return [char for char, count in char_counts.items() if count % 2 != 0] # Test the function with sample input test_str = "geekforgeeks is best for geeks" print("The original string is : " + str(test_str)) res = odd_freq_chars(test_str) print("The Odd Frequency Characters are :" + str(res)) #Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's [END]
Python - Odd Frequency Character
https://www.geeksforgeeks.org/python-odd-frequency-characters/?ref=leftbar-rightbar
# Python3 code to demonstrate working of# Odd Frequency Characters??????from collections import defaultdict, Counter??????# initializing stringtest_str = 'geekforgeeks is best for geeks'??????# printing original "The original string is : " + str(test_str))??????# Odd Frequency Characters using defaultdictchar_count = defaultdict(int)for c in test_str:????????????????????????char_count += 1??????odd_freq_chars = % 2 != 0]?"The Odd Frequency Characters are : " + str(odd_freq_chars))??????# Odd Frequency Characters using Counterchar_count = Counter(test_str)??????odd_freq_chars = % 2 != 0]??????# printing "The Odd Frequency Characters are : " + str(odd_freq_chars))
#Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's
Python - Odd Frequency Character # Python3 code to demonstrate working of# Odd Frequency Characters??????from collections import defaultdict, Counter??????# initializing stringtest_str = 'geekforgeeks is best for geeks'??????# printing original "The original string is : " + str(test_str))??????# Odd Frequency Characters using defaultdictchar_count = defaultdict(int)for c in test_str:????????????????????????char_count += 1??????odd_freq_chars = % 2 != 0]?"The Odd Frequency Characters are : " + str(odd_freq_chars))??????# Odd Frequency Characters using Counterchar_count = Counter(test_str)??????odd_freq_chars = % 2 != 0]??????# printing "The Odd Frequency Characters are : " + str(odd_freq_chars)) #Input : test_str = 'geekforgeeks'?????? #Output : ['r', 'o', 'f', 's [END]
Python - Specific Characters Frequency in String List
https://www.geeksforgeeks.org/python-specific-characters-frequency-in-string-list/
# Python3 code to demonstrate working of # Specific Characters Frequency in String List # Using join() + Counter() from collections import Counter # initializing lists test_list = ["geeksforgeeks is best for geeks"] # printing original list print("The original list : " + str(test_list)) # char list chr_list = ["e", "b", "g"] # dict comprehension to retrieve on certain Frequencies res = { key: val for key, val in dict(Counter("".join(test_list))).items() if key in chr_list } # printing result print("Specific Characters Frequencies : " + str(res))
#Output : The original list : ['geeksforgeeks is best for geeks']
Python - Specific Characters Frequency in String List # Python3 code to demonstrate working of # Specific Characters Frequency in String List # Using join() + Counter() from collections import Counter # initializing lists test_list = ["geeksforgeeks is best for geeks"] # printing original list print("The original list : " + str(test_list)) # char list chr_list = ["e", "b", "g"] # dict comprehension to retrieve on certain Frequencies res = { key: val for key, val in dict(Counter("".join(test_list))).items() if key in chr_list } # printing result print("Specific Characters Frequencies : " + str(res)) #Output : The original list : ['geeksforgeeks is best for geeks'] [END]
Python - Specific Characters Frequency in String List
https://www.geeksforgeeks.org/python-specific-characters-frequency-in-string-list/
# Python3 code to demonstrate working of # Specific Characters Frequency in String List # Using chain.from_iterable() + Counter() + dictionary comprehension from collections import Counter from itertools import chain # initializing lists test_list = ["geeksforgeeks is best for geeks"] # printing original list print("The original list : " + str(test_list)) # char list chr_list = ["e", "b", "g"] # dict comprehension to retrieve on certain Frequencies # from_iterable to flatten / join res = { key: val for key, val in dict(Counter(chain.from_iterable(test_list))).items() if key in chr_list } # printing result print("Specific Characters Frequencies : " + str(res))
#Output : The original list : ['geeksforgeeks is best for geeks']
Python - Specific Characters Frequency in String List # Python3 code to demonstrate working of # Specific Characters Frequency in String List # Using chain.from_iterable() + Counter() + dictionary comprehension from collections import Counter from itertools import chain # initializing lists test_list = ["geeksforgeeks is best for geeks"] # printing original list print("The original list : " + str(test_list)) # char list chr_list = ["e", "b", "g"] # dict comprehension to retrieve on certain Frequencies # from_iterable to flatten / join res = { key: val for key, val in dict(Counter(chain.from_iterable(test_list))).items() if key in chr_list } # printing result print("Specific Characters Frequencies : " + str(res)) #Output : The original list : ['geeksforgeeks is best for geeks'] [END]
Python - Specific Characters Frequency in String List
https://www.geeksforgeeks.org/python-specific-characters-frequency-in-string-list/
# Python3 code to demonstrate working of # Specific Characters Frequency in String List # initializing lists test_list = ["geeksforgeeks is best for geeks"] # printing original list print("The original list : " + str(test_list)) # char list chr_list = ["e", "b", "g"] d = dict() for i in chr_list: d[i] = test_list[0].count(i) res = d # printing result print("Specific Characters Frequencies : " + str(res))
#Output : The original list : ['geeksforgeeks is best for geeks']
Python - Specific Characters Frequency in String List # Python3 code to demonstrate working of # Specific Characters Frequency in String List # initializing lists test_list = ["geeksforgeeks is best for geeks"] # printing original list print("The original list : " + str(test_list)) # char list chr_list = ["e", "b", "g"] d = dict() for i in chr_list: d[i] = test_list[0].count(i) res = d # printing result print("Specific Characters Frequencies : " + str(res)) #Output : The original list : ['geeksforgeeks is best for geeks'] [END]
Python - Specific Characters Frequency in String List
https://www.geeksforgeeks.org/python-specific-characters-frequency-in-string-list/
# Python3 code to demonstrate working of # Specific Characters Frequency in String List import operator as op # initializing lists test_list = ["geeksforgeeks is best for geeks"] # printing original list print("The original list : " + str(test_list)) # char list chr_list = ["e", "b", "g"] d = dict() for i in chr_list: d[i] = op.countOf(test_list[0], i) res = d # printing result print("Specific Characters Frequencies : " + str(res))
#Output : The original list : ['geeksforgeeks is best for geeks']
Python - Specific Characters Frequency in String List # Python3 code to demonstrate working of # Specific Characters Frequency in String List import operator as op # initializing lists test_list = ["geeksforgeeks is best for geeks"] # printing original list print("The original list : " + str(test_list)) # char list chr_list = ["e", "b", "g"] d = dict() for i in chr_list: d[i] = op.countOf(test_list[0], i) res = d # printing result print("Specific Characters Frequencies : " + str(res)) #Output : The original list : ['geeksforgeeks is best for geeks'] [END]
Python - Specific Characters Frequency in String List
https://www.geeksforgeeks.org/python-specific-characters-frequency-in-string-list/
# initializing lists test_list = ["geeksforgeeks is best for geeks"] # printing original list print("The original list : " + str(test_list)) # char list chr_list = ["e", "b", "g"] # initializing dictionary for result res = {} # loop through each character in the test_list and count their frequency for char in "".join(test_list): if char in chr_list: if char in res: res[char] += 1 else: res[char] = 1 # printing result print("Specific Characters Frequencies : " + str(res))
#Output : The original list : ['geeksforgeeks is best for geeks']
Python - Specific Characters Frequency in String List # initializing lists test_list = ["geeksforgeeks is best for geeks"] # printing original list print("The original list : " + str(test_list)) # char list chr_list = ["e", "b", "g"] # initializing dictionary for result res = {} # loop through each character in the test_list and count their frequency for char in "".join(test_list): if char in chr_list: if char in res: res[char] += 1 else: res[char] = 1 # printing result print("Specific Characters Frequencies : " + str(res)) #Output : The original list : ['geeksforgeeks is best for geeks'] [END]
Python | Frequency of numbers in String
https://www.geeksforgeeks.org/python-frequency-of-numbers-in-string/
# Python3 code to demonstrate working of # Frequency of numbers in String # Using re.findall() + len() import re # initializing string test_str = "geeks4feeks is No. 1 4 geeks" # printing original string print("The original string is : " + test_str) # Frequency of numbers in String # Using re.findall() + len() res = len(re.findall(r"\d+", test_str)) # printing result print("Count of numerics in string : " + str(res))
#Output : The original string is : geeks4feeks is No. 1 4 geeks
Python | Frequency of numbers in String # Python3 code to demonstrate working of # Frequency of numbers in String # Using re.findall() + len() import re # initializing string test_str = "geeks4feeks is No. 1 4 geeks" # printing original string print("The original string is : " + test_str) # Frequency of numbers in String # Using re.findall() + len() res = len(re.findall(r"\d+", test_str)) # printing result print("Count of numerics in string : " + str(res)) #Output : The original string is : geeks4feeks is No. 1 4 geeks [END]
Python | Frequency of numbers in String
https://www.geeksforgeeks.org/python-frequency-of-numbers-in-string/
# Python3 code to demonstrate working of # Frequency of numbers in String # Using re.findall() + sum() import re # initializing string test_str = "geeks4feeks is No. 1 4 geeks" # printing original string print("The original string is : " + test_str) # Frequency of numbers in String # Using re.findall() + sum() res = sum(1 for _ in re.finditer(r"\d+", test_str)) # printing result print("Count of numerics in string : " + str(res))
#Output : The original string is : geeks4feeks is No. 1 4 geeks
Python | Frequency of numbers in String # Python3 code to demonstrate working of # Frequency of numbers in String # Using re.findall() + sum() import re # initializing string test_str = "geeks4feeks is No. 1 4 geeks" # printing original string print("The original string is : " + test_str) # Frequency of numbers in String # Using re.findall() + sum() res = sum(1 for _ in re.finditer(r"\d+", test_str)) # printing result print("Count of numerics in string : " + str(res)) #Output : The original string is : geeks4feeks is No. 1 4 geeks [END]
Python | Frequency of numbers in String
https://www.geeksforgeeks.org/python-frequency-of-numbers-in-string/
# Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = "geeks4feeks is No. 1 4 geeks" # printing original string print("The original string is : " + test_str) # Frequency of numbers in String res = 0 for i in test_str: if i.isdigit(): res += 1 # printing result print("Count of numerics in string : " + str(res))
#Output : The original string is : geeks4feeks is No. 1 4 geeks
Python | Frequency of numbers in String # Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = "geeks4feeks is No. 1 4 geeks" # printing original string print("The original string is : " + test_str) # Frequency of numbers in String res = 0 for i in test_str: if i.isdigit(): res += 1 # printing result print("Count of numerics in string : " + str(res)) #Output : The original string is : geeks4feeks is No. 1 4 geeks [END]
Python | Frequency of numbers in String
https://www.geeksforgeeks.org/python-frequency-of-numbers-in-string/
# Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = "geeks4feeks is No. 1 4 geeks" # printing original string print("The original string is : " + test_str) # Frequency of numbers in String res = 0 digits = "0123456789" for i in test_str: if i in digits: res += 1 # printing result print("Count of numerics in string : " + str(res))
#Output : The original string is : geeks4feeks is No. 1 4 geeks
Python | Frequency of numbers in String # Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = "geeks4feeks is No. 1 4 geeks" # printing original string print("The original string is : " + test_str) # Frequency of numbers in String res = 0 digits = "0123456789" for i in test_str: if i in digits: res += 1 # printing result print("Count of numerics in string : " + str(res)) #Output : The original string is : geeks4feeks is No. 1 4 geeks [END]
Python | Frequency of numbers in String
https://www.geeksforgeeks.org/python-frequency-of-numbers-in-string/
# Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = "geeks4feeks is No. 1 4 geeks" # printing original string print("The original string is : " + test_str) # Frequency of numbers in String res = len(list(filter(lambda x: x.isdigit(), test_str))) # printing result print("Count of numerics in string : " + str(res))
#Output : The original string is : geeks4feeks is No. 1 4 geeks
Python | Frequency of numbers in String # Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = "geeks4feeks is No. 1 4 geeks" # printing original string print("The original string is : " + test_str) # Frequency of numbers in String res = len(list(filter(lambda x: x.isdigit(), test_str))) # printing result print("Count of numerics in string : " + str(res)) #Output : The original string is : geeks4feeks is No. 1 4 geeks [END]
Python | Frequency of numbers in String
https://www.geeksforgeeks.org/python-frequency-of-numbers-in-string/
# Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = "geeks4feeks is No. 1 4 geeks" # printing original string print("The original string is : " + test_str) # Frequency of numbers in String res = sum(map(str.isdigit, test_str)) # printing result print("Count of numerics in string : " + str(res))
#Output : The original string is : geeks4feeks is No. 1 4 geeks
Python | Frequency of numbers in String # Python3 code to demonstrate working of # Frequency of numbers in String # initializing string test_str = "geeks4feeks is No. 1 4 geeks" # printing original string print("The original string is : " + test_str) # Frequency of numbers in String res = sum(map(str.isdigit, test_str)) # printing result print("Count of numerics in string : " + str(res)) #Output : The original string is : geeks4feeks is No. 1 4 geeks [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
// C++ program to check if a string// contains any special character??????// import required packages#include <iostream>#include <regex>using namespace std;??????// Function checks if the string// contains any special charactervoid run(string str){??????????????????????????????????????????????????????"[@_!#$%^&*()<>?/|}{~:]");??????????????????????????????// Pass the string in regex_search????????????????????????// method????????????????????????if(r"String is accepted";????????????????????????else????????"String is not accepted.";}??????// Driver Codeint main(){??????????????????????????????????????????????????????"Geeks$For$Geeks";??????????????????????????????????????????????????????// Calling run function????????????????????????run(str);???????
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character // C++ program to check if a string// contains any special character??????// import required packages#include <iostream>#include <regex>using namespace std;??????// Function checks if the string// contains any special charactervoid run(string str){??????????????????????????????????????????????????????"[@_!#$%^&*()<>?/|}{~:]");??????????????????????????????// Pass the string in regex_search????????????????????????// method????????????????????????if(r"String is accepted";????????????????????????else????????"String is not accepted.";}??????// Driver Codeint main(){??????????????????????????????????????????????????????"Geeks$For$Geeks";??????????????????????????????????????????????????????// Calling run function????????????????????????run(str);??????? #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
# Python3 program to check if a string # contains any special character # import required package import re # Function checks if the string # contains any special character def run(string): # Make own character set and pass # this as argument in compile method regex = re.compile("[@_!#$%^&*()<>?/\|}{~:]") # Pass the string in search # method of regex object. if regex.search(string) == None: print("String is accepted") else: print("String is not accepted.") # Driver Code if __name__ == "__main__": # Enter the string string = "Geeks$For$Geeks" # calling run function run(string)
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character # Python3 program to check if a string # contains any special character # import required package import re # Function checks if the string # contains any special character def run(string): # Make own character set and pass # this as argument in compile method regex = re.compile("[@_!#$%^&*()<>?/\|}{~:]") # Pass the string in search # method of regex object. if regex.search(string) == None: print("String is accepted") else: print("String is not accepted.") # Driver Code if __name__ == "__main__": # Enter the string string = "Geeks$For$Geeks" # calling run function run(string) #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
<?Php// PHP program to check if a string// contains any special character??????// Function checks if the string// contains any special characterfunction run($string){????????????????????????$regex = preg_match('[@_!#$%^&*()<>?/\|}{~:]',???????????????????????????????????????????????????????????????????????????????????????????????????????????????"String is accepted");????????????????????????????????????????????????????"String is not accepted.");}??????// Driver Code??????// Enter the string$string = 'Geeks$For$Geeks';??????// calling run functionrun($string);??????// This code is contribute
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character <?Php// PHP program to check if a string// contains any special character??????// Function checks if the string// contains any special characterfunction run($string){????????????????????????$regex = preg_match('[@_!#$%^&*()<>?/\|}{~:]',???????????????????????????????????????????????????????????????????????????????????????????????????????????????"String is accepted");????????????????????????????????????????????????????"String is not accepted.");}??????// Driver Code??????// Enter the string$string = 'Geeks$For$Geeks';??????// calling run functionrun($string);??????// This code is contribute #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
import java.util.regex.Matcher;import java.util.regex.Pattern;??????public class Main {????????????????????????public static void main(String[] args)??????????????????"Geeks$For$Geeks";????????????????????????????????????????????????Pattern pattern??????????"[@_!#$%^&*()<>?/|}{~:]");????????????????????????????????????????????????Matcher matcher = pattern.matcher(str);???????????????????????????????????????????????????"String is accepted");????????????????????????????????????????????????}?????????????????????????????????"String is not accepted");????????????????????????????????????????????????}????????????????????????
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character import java.util.regex.Matcher;import java.util.regex.Pattern;??????public class Main {????????????????????????public static void main(String[] args)??????????????????"Geeks$For$Geeks";????????????????????????????????????????????????Pattern pattern??????????"[@_!#$%^&*()<>?/|}{~:]");????????????????????????????????????????????????Matcher matcher = pattern.matcher(str);???????????????????????????????????????????????????"String is accepted");????????????????????????????????????????????????}?????????????????????????????????"String is not accepted");????????????????????????????????????????????????}???????????????????????? #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
// C# program to check if a string// contains any special character??????using System;using System.Text.RegularExpressions;??????class Program {????????????????????????// Function checks if the string????????????????????????// contains any special character????????????????????????static void Run(string str)????????????????????????{?????????"[@_!#$%^&*()<>?/|}{~:]");????????????????????????????????????????????????// Pass the string in regex.IsMatch????????????????????????????????????????????????// method??????????????????"String is accepted");????????????????????????????????????????????????else??????????"String is not accepted.");????????????????????????}??????????????????????????????// Driver Code????????????????????????static void Main()?????????????????????"Geeks$For$Geeks";??????????????????????????????????????????????????????// Calling Run function????????????????????????????????????
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character // C# program to check if a string// contains any special character??????using System;using System.Text.RegularExpressions;??????class Program {????????????????????????// Function checks if the string????????????????????????// contains any special character????????????????????????static void Run(string str)????????????????????????{?????????"[@_!#$%^&*()<>?/|}{~:]");????????????????????????????????????????????????// Pass the string in regex.IsMatch????????????????????????????????????????????????// method??????????????????"String is accepted");????????????????????????????????????????????????else??????????"String is not accepted.");????????????????????????}??????????????????????????????// Driver Code????????????????????????static void Main()?????????????????????"Geeks$For$Geeks";??????????????????????????????????????????????????????// Calling Run function???????????????????????????????????? #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
function hasSpecialChar(str) {????????????let regex = /[@!#$%^&*()_+\-=\"\\|,.<>\/?]/;????????????return regex.test(str);}????"Geeks$For$Geeks";if (!hasSpecialChar(str)) {????????????cons"String is accepted");} else {????????????cons"String is not accepted");}
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character function hasSpecialChar(str) {????????????let regex = /[@!#$%^&*()_+\-=\"\\|,.<>\/?]/;????????????return regex.test(str);}????"Geeks$For$Geeks";if (!hasSpecialChar(str)) {????????????cons"String is accepted");} else {????????????cons"String is not accepted");} #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
// C++ code// to check if any special character is present// in a given string or not#include <iostream>#include <string>using namespace std;??????int main(){??????????????????????????????// Input s"Geeks$For$Geeks";????????????????????????int c = 0;??????????????????"[@_!#$%^&*()<>?}{~:]"; // special character set????????????????????????for (int i = 0; i < n.size(); i++) {??????????????????????????????????????????????????????// checking if any special character is present in????????????????????????????????????????????????// given string or not????????????????????????????????????????????????if (s.find(n[i]) != string::npos) {????????????????????????????????????????????????????????????????????????// if special character found then add 1 to the??????????????????????????????????????????????????????????????????"string is not accepted";????????????????????????}??????????????????????"string is accepted";????????????????????????}??????????????????????????????return 0;}??????/
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character // C++ code// to check if any special character is present// in a given string or not#include <iostream>#include <string>using namespace std;??????int main(){??????????????????????????????// Input s"Geeks$For$Geeks";????????????????????????int c = 0;??????????????????"[@_!#$%^&*()<>?}{~:]"; // special character set????????????????????????for (int i = 0; i < n.size(); i++) {??????????????????????????????????????????????????????// checking if any special character is present in????????????????????????????????????????????????// given string or not????????????????????????????????????????????????if (s.find(n[i]) != string::npos) {????????????????????????????????????????????????????????????????????????// if special character found then add 1 to the??????????????????????????????????????????????????????????????????"string is not accepted";????????????????????????}??????????????????????"string is accepted";????????????????????????}??????????????????????????????return 0;}??????/ #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
// Java code to check if any special character is present// in a given string or notimport java.util.*;class Main {????????????????????????public static void main(String[] args)????????????????????????{??????????????????????????"Geeks$For$Geeks";????????????????????????????????????????????????int c"[@_!#$%^&*()<>?}{~:]"; // special??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????// character set????????????????????????????????????????????????for (int i = 0; i < n.length(); i++) {??????????????????????????????????????????????????????????????????????????????// checking if any special character is present????????????????????????????????????????????????????????????????????????// in given string or not????????????????????????????????????????????????????????????????????????if (s.indexOf(n.charAt(i)) != -1) {????????????????????????????????????"string is not accepted");????????????????????????????????????????????????}?????????????????????????????????"string is accepted");???????????????????????????
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character // Java code to check if any special character is present// in a given string or notimport java.util.*;class Main {????????????????????????public static void main(String[] args)????????????????????????{??????????????????????????"Geeks$For$Geeks";????????????????????????????????????????????????int c"[@_!#$%^&*()<>?}{~:]"; // special??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????// character set????????????????????????????????????????????????for (int i = 0; i < n.length(); i++) {??????????????????????????????????????????????????????????????????????????????// checking if any special character is present????????????????????????????????????????????????????????????????????????// in given string or not????????????????????????????????????????????????????????????????????????if (s.indexOf(n.charAt(i)) != -1) {????????????????????????????????????"string is not accepted");????????????????????????????????????????????????}?????????????????????????????????"string is accepted");??????????????????????????? #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
# Python code # to check if any special character is present # in a given string or not # input string n = "Geeks$For$Geeks" n.split() c = 0 s = "[@_!#$%^&*()<>?/\|}{~:]" # special character set for i in range(len(n)): # checking if any special character is present in given string or not if n[i] in s: c += 1 # if special character found then add 1 to the c # if c value is greater than 0 then print no # means special character is found in string if c: print("string is not accepted") else: print("string accepted") # this code is contributed by gangarajula laxmi
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character # Python code # to check if any special character is present # in a given string or not # input string n = "Geeks$For$Geeks" n.split() c = 0 s = "[@_!#$%^&*()<>?/\|}{~:]" # special character set for i in range(len(n)): # checking if any special character is present in given string or not if n[i] in s: c += 1 # if special character found then add 1 to the c # if c value is greater than 0 then print no # means special character is found in string if c: print("string is not accepted") else: print("string accepted") # this code is contributed by gangarajula laxmi #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
// JavaScript code// to check if any special character is present// in a given string or notconst n = "Geeks$For$Geeks";let c = 0;const s = "[@_!#$%^&*()<>?}{~:]"; // special character set??????for (let i = 0; i < n.length; i++) {????????????????????????// checking if any special character is present in given string or not????????????????????????if (s.includes(n[i])) {????????????????????????????????????????????????// if special character found then add 1 to the c????????????????????????????????????????????????c++;????????????????????????}}????"string is not accepted");} else {????????????????????"string is accepted");}
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character // JavaScript code// to check if any special character is present// in a given string or notconst n = "Geeks$For$Geeks";let c = 0;const s = "[@_!#$%^&*()<>?}{~:]"; // special character set??????for (let i = 0; i < n.length; i++) {????????????????????????// checking if any special character is present in given string or not????????????????????????if (s.includes(n[i])) {????????????????????????????????????????????????// if special character found then add 1 to the c????????????????????????????????????????????????c++;????????????????????????}}????"string is not accepted");} else {????????????????????"string is accepted");} #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
using System;??????class Program {????????????????????????static void Main(string[] args)????????????????????????{??????????????????????"Geeks$For$Geeks";????????????????????????????????????????????????int c"[@_!#$%^&*()<>?}{~:]"; // special??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????// character set??????????????????????????????????????????????????????for (int i = 0; i < n.Length; i++) {????????????????????????????????????????????????????????????????????????// checking if any special character is present????????????????????????????????????????????????????????????????????????// in given string or not????????????????????????????????????????????????????????????????????????if (s.IndexOf(n[i]) != -1) {???????????????????????????????????"string is not accepted");????????????????????????????????????????????????}????????????????????????????????"string is accepted");???????????????????????????
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character using System;??????class Program {????????????????????????static void Main(string[] args)????????????????????????{??????????????????????"Geeks$For$Geeks";????????????????????????????????????????????????int c"[@_!#$%^&*()<>?}{~:]"; // special??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????// character set??????????????????????????????????????????????????????for (int i = 0; i < n.Length; i++) {????????????????????????????????????????????????????????????????????????// checking if any special character is present????????????????????????????????????????????????????????????????????????// in given string or not????????????????????????????????????????????????????????????????????????if (s.IndexOf(n[i]) != -1) {???????????????????????????????????"string is not accepted");????????????????????????????????????????????????}????????????????????????????????"string is accepted");??????????????????????????? #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
#include <iostream>#include <string>??????using namespace std;??????bool hasSpecialChar(string s){????????????????????????for (char c : s) {????????????????????????????????????????????????if (!(isalpha(c) || isdigit(c) || c == ' ')) {????????????????????????????????????????????????????????????"Hello World";????????????????????????if (hasSpecialChar(s)) {????????"The string contains special characters."??????????????????????????<< endl;????????}????????else {????????????????cout << "The string does not contain special "????????????????????????????????"characters."??????????????????????????<< endl;????????}??????????s = "Hello@World";????????????????????????if (hasSpecialChar(s)) {????????"The string contains special characters."??????????????????????????<< endl;????????}????????else {????????????????cout << "The string does not contain special "????????????????????????????????"characters."??????????????????????????<< endl;????????}??????????return 0;}
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character #include <iostream>#include <string>??????using namespace std;??????bool hasSpecialChar(string s){????????????????????????for (char c : s) {????????????????????????????????????????????????if (!(isalpha(c) || isdigit(c) || c == ' ')) {????????????????????????????????????????????????????????????"Hello World";????????????????????????if (hasSpecialChar(s)) {????????"The string contains special characters."??????????????????????????<< endl;????????}????????else {????????????????cout << "The string does not contain special "????????????????????????????????"characters."??????????????????????????<< endl;????????}??????????s = "Hello@World";????????????????????????if (hasSpecialChar(s)) {????????"The string contains special characters."??????????????????????????<< endl;????????}????????else {????????????????cout << "The string does not contain special "????????????????????????????????"characters."??????????????????????????<< endl;????????}??????????return 0;} #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
class Main {????????????????????????public static boolean hasSpecialChar(String s)????????????????????????{????????????????????????????????????????????????for (int i = 0; i < s.length(); i++) {????????????????????????????????????????????????????????????????????????char c = s.charAt(i);????????????????????????????????????????????????????????????????????????if (!(Character.isLetterOrDigit(c)????????????????????????????????????????????????????????????????????????????????????????????????????"Hello World";????????????????????????????????????????????????if (hasSpecialChar(s1)) {???????????????????????????????????????????"The string contains special characters.");????????????????????????????????????????????????}????????????????????????????????????????????????else {???????????"The string does not contain special characters.");?????????????????????????????????????????????"Hello@World";????????????????????????????????????????????????if (hasSpecialChar(s2)) {???????????????????????????????????????????"The string contains special characters.");????????????????????????????????????????????????}????????????????????????????????????????????????else {???????????"The string does not contain special characters.");???????????????????????????
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character class Main {????????????????????????public static boolean hasSpecialChar(String s)????????????????????????{????????????????????????????????????????????????for (int i = 0; i < s.length(); i++) {????????????????????????????????????????????????????????????????????????char c = s.charAt(i);????????????????????????????????????????????????????????????????????????if (!(Character.isLetterOrDigit(c)????????????????????????????????????????????????????????????????????????????????????????????????????"Hello World";????????????????????????????????????????????????if (hasSpecialChar(s1)) {???????????????????????????????????????????"The string contains special characters.");????????????????????????????????????????????????}????????????????????????????????????????????????else {???????????"The string does not contain special characters.");?????????????????????????????????????????????"Hello@World";????????????????????????????????????????????????if (hasSpecialChar(s2)) {???????????????????????????????????????????"The string contains special characters.");????????????????????????????????????????????????}????????????????????????????????????????????????else {???????????"The string does not contain special characters.");??????????????????????????? #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
def has_special_char(s): for c in s: if not (c.isalpha() or c.isdigit() or c == " "): return True return False # Test the function s = "Hello World" if has_special_char(s): print("The string contains special characters.") else: print("The string does not contain special characters.") s = "Hello@World" if has_special_char(s): print("The string contains special characters.") else: print("The string does not contain special characters.") # This code is contributed by Edula Vinay Kumar Reddy
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character def has_special_char(s): for c in s: if not (c.isalpha() or c.isdigit() or c == " "): return True return False # Test the function s = "Hello World" if has_special_char(s): print("The string contains special characters.") else: print("The string does not contain special characters.") s = "Hello@World" if has_special_char(s): print("The string contains special characters.") else: print("The string does not contain special characters.") # This code is contributed by Edula Vinay Kumar Reddy #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
using System;??????class GFG {????????????????????????// This method checks if a string contains any special????????????????????????// characters.????????????????????????public static bool HasSpecialChar(string s)????????????????????????{????????????????????????????????????????????????// Iterate over each character in the string.????????????????????????????????????????????????for (int i = 0; i < s.Length; i++) {????????????????????????????????????????????????????????????????????????char c = s[i];????????????????????????????????????????????????????????????????????????// If the character is not a letter, digit, or????????????????????????????????????????????????????????????????????????// space, it is a special character.????????????????????????????????????????????????????????????????????????if (!(char"Hello World";????????????????????????????????????????????????// Check if s1 contains any special characters and????????????????????????????????????????????????// print the result.????????????????????????????????????????????????if "The string contains special characters.");????????????????????????????????????????????????}????????????????????????????????????????????????else {??????????"The string does not contain special characters.");?????????????????????????????????????????????"Hello@World";????????????????????????????????????????????????// Check if s2 contains any special characters and????????????????????????????????????????????????// print the result.????????????????????????????????????????????????if "The string contains special characters.");????????????????????????????????????????????????}????????????????????????????????????????????????else {??????????"The string does not contain special characters.");???????????????????????????
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character using System;??????class GFG {????????????????????????// This method checks if a string contains any special????????????????????????// characters.????????????????????????public static bool HasSpecialChar(string s)????????????????????????{????????????????????????????????????????????????// Iterate over each character in the string.????????????????????????????????????????????????for (int i = 0; i < s.Length; i++) {????????????????????????????????????????????????????????????????????????char c = s[i];????????????????????????????????????????????????????????????????????????// If the character is not a letter, digit, or????????????????????????????????????????????????????????????????????????// space, it is a special character.????????????????????????????????????????????????????????????????????????if (!(char"Hello World";????????????????????????????????????????????????// Check if s1 contains any special characters and????????????????????????????????????????????????// print the result.????????????????????????????????????????????????if "The string contains special characters.");????????????????????????????????????????????????}????????????????????????????????????????????????else {??????????"The string does not contain special characters.");?????????????????????????????????????????????"Hello@World";????????????????????????????????????????????????// Check if s2 contains any special characters and????????????????????????????????????????????????// print the result.????????????????????????????????????????????????if "The string contains special characters.");????????????????????????????????????????????????}????????????????????????????????????????????????else {??????????"The string does not contain special characters.");??????????????????????????? #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
function hasSpecialChar(s) {????????????????????????for (let i = 0; i < s.length; i++) {????????????????????????????????????????????????const c = s.charAt(i);????????????????????????????????????????????????if (!(c.match(/^[a-zA-Z0-9 ]+$/))) {??????????????"Hello World";if (hasSpecialChar(s)) {????????????????????"The string contains special characters.");} else {????????????????????"The string does not contain special characters.");}??????"Hello@World";if (hasSpecialChar(s)) {????????????????????"The string contains special characters.");} else {????????????????????"The string does not contain special characters.");}
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character function hasSpecialChar(s) {????????????????????????for (let i = 0; i < s.length; i++) {????????????????????????????????????????????????const c = s.charAt(i);????????????????????????????????????????????????if (!(c.match(/^[a-zA-Z0-9 ]+$/))) {??????????????"Hello World";if (hasSpecialChar(s)) {????????????????????"The string contains special characters.");} else {????????????????????"The string does not contain special characters.");}??????"Hello@World";if (hasSpecialChar(s)) {????????????????????"The string contains special characters.");} else {????????????????????"The string does not contain special characters.");} #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Python | Program to check if a string contains any special character
https://www.geeksforgeeks.org/python-program-check-string-contains-special-character/
import string def check_string(s): for c in s: if c in string.punctuation: print("String is not accepted") return print("String is accepted") # Example usage check_string("Geeks$For$Geeks") # Output: String is not accepted check_string("Geeks For Geeks") # Output: String is accepted
#Input : Geeks$For$Geeks #Output : String is not accepted.
Python | Program to check if a string contains any special character import string def check_string(s): for c in s: if c in string.punctuation: print("String is not accepted") return print("String is accepted") # Example usage check_string("Geeks$For$Geeks") # Output: String is not accepted check_string("Geeks For Geeks") # Output: String is accepted #Input : Geeks$For$Geeks #Output : String is not accepted. [END]
Generating random strings until a given string is generated
https://www.geeksforgeeks.org/python-program-match-string-random-strings-length/
# Python program to generate and match??????# the string from all random strings# of same length????????????# Importing string, random# and time modulesimport stringimport randomimport time????????????# all possible characters including??????# lowercase, uppercase and special symbolspossibleCharacters = string.ascii_lowercase + string.digits +???????????????????????????????????????????????????????????????????????????????????????"geek"????# To take input from user# t = input(str("Enter your target text: "))????????????attemptThis = ''.join(random.choice(possibleCharacters)????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????for i in range(len(t)))attemptNext = ''????????????completed = Falseiteration = 0????????????# Iterate while completed is falsewhile completed == False:????????????????????????print(attemptThis)????????????????????????????????????????????????????????????attemptNext = ''????????????????????????completed = True????????????????????????????????????????????????????????????# Fix the index if matches with??????????????????????????????# the strings to be generated????????????????????????for i in range(len(t)):????????????????????????????????????????????????if attemptThis[i] != t[i]:?????????"Target matched after " +?????????????????????????????" iterations")
#Input : GFG
Generating random strings until a given string is generated # Python program to generate and match??????# the string from all random strings# of same length????????????# Importing string, random# and time modulesimport stringimport randomimport time????????????# all possible characters including??????# lowercase, uppercase and special symbolspossibleCharacters = string.ascii_lowercase + string.digits +???????????????????????????????????????????????????????????????????????????????????????"geek"????# To take input from user# t = input(str("Enter your target text: "))????????????attemptThis = ''.join(random.choice(possibleCharacters)????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????for i in range(len(t)))attemptNext = ''????????????completed = Falseiteration = 0????????????# Iterate while completed is falsewhile completed == False:????????????????????????print(attemptThis)????????????????????????????????????????????????????????????attemptNext = ''????????????????????????completed = True????????????????????????????????????????????????????????????# Fix the index if matches with??????????????????????????????# the strings to be generated????????????????????????for i in range(len(t)):????????????????????????????????????????????????if attemptThis[i] != t[i]:?????????"Target matched after " +?????????????????????????????" iterations") #Input : GFG [END]
Find words which are greater than K than given length k
https://www.geeksforgeeks.org/find-words-greater-given-length-k/
// C++ program to find all string// which are greater than given length k??????#include <bits/stdc++.h>using namespace std;??????// function find string greater than// length kvoid string_k(string s, int k){????????????????????????// create an empty s"";????????????????????????// iterate the loop till every space????????????????????????for (int i = 0; i < s.size(); i++) {????????????????????????????????????????????????if (s[i] != ' ')??????????????????????????????????????????????????????????????????????????????// append this sub string in????????????????????????????????????????????????????????????????????????// string w????????????????????????????????????????????????????????????????????????w = w + s[i];???????????????????????????????????????" ";????????????????????????????"";????????????????????????????????????????????????}????????????????????????"geek for geeks";????????????????????????int k = 3;" ";????????????????????????string_k(s, k);????????????????????????return 0;}??????// This code is contri
#Input : str = "hello geeks for geeks is computer science portal"
Find words which are greater than K than given length k // C++ program to find all string// which are greater than given length k??????#include <bits/stdc++.h>using namespace std;??????// function find string greater than// length kvoid string_k(string s, int k){????????????????????????// create an empty s"";????????????????????????// iterate the loop till every space????????????????????????for (int i = 0; i < s.size(); i++) {????????????????????????????????????????????????if (s[i] != ' ')??????????????????????????????????????????????????????????????????????????????// append this sub string in????????????????????????????????????????????????????????????????????????// string w????????????????????????????????????????????????????????????????????????w = w + s[i];???????????????????????????????????????" ";????????????????????????????"";????????????????????????????????????????????????}????????????????????????"geek for geeks";????????????????????????int k = 3;" ";????????????????????????string_k(s, k);????????????????????????return 0;}??????// This code is contri #Input : str = "hello geeks for geeks is computer science portal" [END]
Find words which are greater than K than given length k
https://www.geeksforgeeks.org/find-words-greater-given-length-k/
// C# program to find all string// which are greater than given length k??????using System;??????class GFG {??????????????????????????????// function find string greater than????????????????????????// length k????????????????????????static void string_k(string s, int k)??????????????????"";??????????????????????????????????????????????????????// iterate the loop till every space????????????????????????????????????????????????for (int i = 0; i < s.Length; i++) {????????????????????????????????????????????????????????????????????????if (s[i] != ' ')??????????????????????????????????????????????????????????????????????????????????????????????????????// append this sub string in????????????????????????????????????????????????????????????????????????????????????????????????// string w????????????????????????????????????????????????????????????????????????????????????????????????w = w" ");????????????????????????????????????"";????????????????????????????????????????????????????????????????????????}????????????????????????????????????????????????}???????????????"geek for geeks";????????????????????????????????????????????????in" ";????????????????????????????????????????????????string_k(s, k);????????????????????????}}??????// Thi
#Input : str = "hello geeks for geeks is computer science portal"
Find words which are greater than K than given length k // C# program to find all string// which are greater than given length k??????using System;??????class GFG {??????????????????????????????// function find string greater than????????????????????????// length k????????????????????????static void string_k(string s, int k)??????????????????"";??????????????????????????????????????????????????????// iterate the loop till every space????????????????????????????????????????????????for (int i = 0; i < s.Length; i++) {????????????????????????????????????????????????????????????????????????if (s[i] != ' ')??????????????????????????????????????????????????????????????????????????????????????????????????????// append this sub string in????????????????????????????????????????????????????????????????????????????????????????????????// string w????????????????????????????????????????????????????????????????????????????????????????????????w = w" ");????????????????????????????????????"";????????????????????????????????????????????????????????????????????????}????????????????????????????????????????????????}???????????????"geek for geeks";????????????????????????????????????????????????in" ";????????????????????????????????????????????????string_k(s, k);????????????????????????}}??????// Thi #Input : str = "hello geeks for geeks is computer science portal" [END]
Find words which are greater than K than given length k
https://www.geeksforgeeks.org/find-words-greater-given-length-k/
// Java program to find all string// which are greater than given length k??????import java.io.*;import java.util.*;??????public class GFG {??????????????????????????????// function find string greater than????????????????????????// length k????????????????????????static void string_k(String s, int k)??????????????????"";??????????????????????????????????????????????????????// iterate the loop till every space????????????????????????????????????????????????for (int i = 0; i < s.length(); i++) {????????????????????????????????????????????????????????????????????????if (s.charAt(i) != ' ')??????????????????????????????????????????????????????????????????????????????????????????????????????// append this sub string in????????????????????????????????????????????????????????????????????????????????????????????????// string w????????????????????????????????????????????????????????????????????????????????????????????????w = w + s.charAt(" ");????????????????????????????????????"";????????????????????????????????????????????????????????????????????????}????????????????????????????????????????????????}????????????????????????}??????????"geek for geeks";????????????????????????????????????????????????in" ";????????????????????????????????????????????????string_k(s, k);????????????????????????}}??????// Thi
#Input : str = "hello geeks for geeks is computer science portal"
Find words which are greater than K than given length k // Java program to find all string// which are greater than given length k??????import java.io.*;import java.util.*;??????public class GFG {??????????????????????????????// function find string greater than????????????????????????// length k????????????????????????static void string_k(String s, int k)??????????????????"";??????????????????????????????????????????????????????// iterate the loop till every space????????????????????????????????????????????????for (int i = 0; i < s.length(); i++) {????????????????????????????????????????????????????????????????????????if (s.charAt(i) != ' ')??????????????????????????????????????????????????????????????????????????????????????????????????????// append this sub string in????????????????????????????????????????????????????????????????????????????????????????????????// string w????????????????????????????????????????????????????????????????????????????????????????????????w = w + s.charAt(" ");????????????????????????????????????"";????????????????????????????????????????????????????????????????????????}????????????????????????????????????????????????}????????????????????????}??????????"geek for geeks";????????????????????????????????????????????????in" ";????????????????????????????????????????????????string_k(s, k);????????????????????????}}??????// Thi #Input : str = "hello geeks for geeks is computer science portal" [END]
Find words which are greater than K than given length k
https://www.geeksforgeeks.org/find-words-greater-given-length-k/
# Python program to find all string # which are greater than given length k # function find string greater than length k def string_k(k, str): # create the empty string string = [] # split the string where space is comes text = str.split(" ") # iterate the loop till every substring for x in text: # if length of current sub string # is greater than k then if len(x) > k: # append this sub string in # string list string.append(x) # return string list return string # Driver Program k = 3 str = "geek for geeks" print(string_k(k, str))
#Input : str = "hello geeks for geeks is computer science portal"
Find words which are greater than K than given length k # Python program to find all string # which are greater than given length k # function find string greater than length k def string_k(k, str): # create the empty string string = [] # split the string where space is comes text = str.split(" ") # iterate the loop till every substring for x in text: # if length of current sub string # is greater than k then if len(x) > k: # append this sub string in # string list string.append(x) # return string list return string # Driver Program k = 3 str = "geek for geeks" print(string_k(k, str)) #Input : str = "hello geeks for geeks is computer science portal" [END]
Find words which are greater than K than given length k
https://www.geeksforgeeks.org/find-words-greater-given-length-k/
<?php// PHP program to find all $// which are greater than given length k??????// function find string greater than// length kfunction string_k($s, $k){?????????????????????????????????????????????????????"";??????????????????????????????????????????????????????// iterate the loop till every space????????????????????????for($i = 0; $i < strlen($s); $i++)????????????????????????{????????????????????????????????????????????????if($s[$i] != ' ')??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????// append this sub $in $w????????????????????????????????????????????????????????????????????????$w = $w.$s[$i];???????????????????????????????????" ");?????????????????????????????"";????????????????????????????????????????????????"geek for geeks";$k = 3;$s = $s . " ";string_k($s, $k);??????// This code is contributed by// Manish Shaw (manishshaw
#Input : str = "hello geeks for geeks is computer science portal"
Find words which are greater than K than given length k <?php// PHP program to find all $// which are greater than given length k??????// function find string greater than// length kfunction string_k($s, $k){?????????????????????????????????????????????????????"";??????????????????????????????????????????????????????// iterate the loop till every space????????????????????????for($i = 0; $i < strlen($s); $i++)????????????????????????{????????????????????????????????????????????????if($s[$i] != ' ')??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????// append this sub $in $w????????????????????????????????????????????????????????????????????????$w = $w.$s[$i];???????????????????????????????????" ");?????????????????????????????"";????????????????????????????????????????????????"geek for geeks";$k = 3;$s = $s . " ";string_k($s, $k);??????// This code is contributed by// Manish Shaw (manishshaw #Input : str = "hello geeks for geeks is computer science portal" [END]
Find words which are greater than K than given length k
https://www.geeksforgeeks.org/find-words-greater-given-length-k/
<script>// javascript program to find all string// which are greater than given length k??????????????????????????????// function find string greater than????????????????????????// length k????????????????????????function string_k( s , k) {??????????????"";??????????????????????????????????????????????????????// iterate the loop till every space????????????????????????????????????????????????for (i = 0; i < s.length; i++) {????????????????????????????????????????????????????????????????????????if (s.charAt(i) != ' ')??????????????????????????????????????????????????????????????????????????????????????????????????????// append this sub string in????????????????????????????????????????????????????????????????????????????????????????????????// string w????????????????????????????????????????????????????????????????????????????????????????????????w = w + s.cha" ");????????????????????????????????????"";????????????????????????????????????????????????????????????????????????}????????????????????????????????????"geek for geeks";????????????????????????????????????????????????va" ";????????????????????????????????????????????????string_k(s, k);??????// This code is
#Input : str = "hello geeks for geeks is computer science portal"
Find words which are greater than K than given length k <script>// javascript program to find all string// which are greater than given length k??????????????????????????????// function find string greater than????????????????????????// length k????????????????????????function string_k( s , k) {??????????????"";??????????????????????????????????????????????????????// iterate the loop till every space????????????????????????????????????????????????for (i = 0; i < s.length; i++) {????????????????????????????????????????????????????????????????????????if (s.charAt(i) != ' ')??????????????????????????????????????????????????????????????????????????????????????????????????????// append this sub string in????????????????????????????????????????????????????????????????????????????????????????????????// string w????????????????????????????????????????????????????????????????????????????????????????????????w = w + s.cha" ");????????????????????????????????????"";????????????????????????????????????????????????????????????????????????}????????????????????????????????????"geek for geeks";????????????????????????????????????????????????va" ";????????????????????????????????????????????????string_k(s, k);??????// This code is #Input : str = "hello geeks for geeks is computer science portal" [END]
Find words which are greater than K than given length k
https://www.geeksforgeeks.org/find-words-greater-given-length-k/
#include <iostream>#include <sstream>#include <string>#include <vector>??????using namespace std;??????int main(){??????????????????"hello geeks for geeks is computer "????????????????????????????????????????????"science portal";????????????????????????int length = 4;????????????????????????vector<string> words;????????????????????????stringstream ss(sentence);????????????????????????string word;??????????????????????????????while (ss >> word) {????????????????????????????????????????????????if (word.length() > length) {????????????????????????" ";????????????????????????}???????????????????????????
#Input : str = "hello geeks for geeks is computer science portal"
Find words which are greater than K than given length k #include <iostream>#include <sstream>#include <string>#include <vector>??????using namespace std;??????int main(){??????????????????"hello geeks for geeks is computer "????????????????????????????????????????????"science portal";????????????????????????int length = 4;????????????????????????vector<string> words;????????????????????????stringstream ss(sentence);????????????????????????string word;??????????????????????????????while (ss >> word) {????????????????????????????????????????????????if (word.length() > length) {????????????????????????" ";????????????????????????}??????????????????????????? #Input : str = "hello geeks for geeks is computer science portal" [END]
Find words which are greater than K than given length k
https://www.geeksforgeeks.org/find-words-greater-given-length-k/
using System;using System.Linq;??????class Program{????????????????????????static void Main(string[] args)???????????????????????"hello geeks for geeks is computer science portal";????????????????????????????????????????????????int length = 4;????????????????????????????????????????????????var words = sentence.Split(' ').Where(word => word"[" + string.Join(", ", words.Select(w => "'" + w + "'")) + "]");??????????
#Input : str = "hello geeks for geeks is computer science portal"
Find words which are greater than K than given length k using System;using System.Linq;??????class Program{????????????????????????static void Main(string[] args)???????????????????????"hello geeks for geeks is computer science portal";????????????????????????????????????????????????int length = 4;????????????????????????????????????????????????var words = sentence.Split(' ').Where(word => word"[" + string.Join(", ", words.Select(w => "'" + w + "'")) + "]");?????????? #Input : str = "hello geeks for geeks is computer science portal" [END]
Find words which are greater than K than given length k
https://www.geeksforgeeks.org/find-words-greater-given-length-k/
import java.util.Arrays;??????public class Main {????????????????????????public static void main(String[] args)????????????????????????{?????????????????????"hello geeks for geeks is computer science portal";????????????????????????????????????????????????int length = 4;????????????????????????????????????????????????Strin" "))????????????????????????????????????????????????????????????????????????????????????????????????????????????.filter(word -> word.length() > length)????????????????????????????????????????????????????????????
#Input : str = "hello geeks for geeks is computer science portal"
Find words which are greater than K than given length k import java.util.Arrays;??????public class Main {????????????????????????public static void main(String[] args)????????????????????????{?????????????????????"hello geeks for geeks is computer science portal";????????????????????????????????????????????????int length = 4;????????????????????????????????????????????????Strin" "))????????????????????????????????????????????????????????????????????????????????????????????????????????????.filter(word -> word.length() > length)???????????????????????????????????????????????????????????? #Input : str = "hello geeks for geeks is computer science portal" [END]