Programming Articles - Page 345 of 3363

__future__ Module in Python

Sarika Singh
Updated on 13-Jul-2025 00:03:01

484 Views

What is the __future__ Module? The __future__ is a built-in Python module that is used to import features from newer versions of Python into older versions. This helps you to write code that will work the same way in both old and new versions of Python. It is mainly used when you are migrating code from Python 2 to Python 3, or when you want to try out new features before they become standard (default) in future releases. For example, in older Python versions, dividing two integers would return an integer. But if you import the division feature like this: ... Read More

Swift Program to Get key from Dictionary using the value

Ankita Saini
Updated on 09-May-2023 10:35:05

3K+ Views

A dictionary is a collection of key-value pairs. Which means keys and values are interrelated with each other. So using a value we can easily get single or multiple keys from the given dictionary. Algorithm Step 1 − Create a dictionary with key-value pairs. Step 2 − Then create a function which will return the key from the dictionary using the value. Step 3 − Inside the function we run a for-in loop which iterates over the key-value pairs of the given dictionary. Step 4 − Now check if the current value is equal to the specified value. If ... Read More

Swift Program to Find the Frequency of Characters in a String

Ankita Saini
Updated on 09-May-2023 10:31:24

1K+ Views

In Swift, the frequency of all the characters in a string means how much time a character repeats in the given string. For example, “Swift tutorial”, here “t” repeats 3 times in the given string, “i” repeat 2 times in the given string. So in this article, we are going to find the frequency of all the characters in the string. Algorithm Step 1 − Create a variable to store a string. Step 2 − Create a dictionary to store the character and its count. Step 3 − Run a for loop to iterate over each character in the ... Read More

Swift Program to delete suffix substring from the given string

Ankita Saini
Updated on 09-May-2023 10:26:37

2K+ Views

To delete the suffix substring from the given string first, we check if the given substring is present in the specified string or not using the inbuilt hasSuffix() function. Then find the index of the suffix substring using the inbuilt index() function and finally remove the suffix substring. Input String = “Siya love cooking” Substring = “cooking” Output “Siya love” Here, the specified substring is found in the given string, so in the resultant string, we removed the substring from the end of the input string. Algorithm Step 1 − Create a string. Step 2 − Create ... Read More

Swift Program to delete prefix substring from the given string

Ankita Saini
Updated on 09-May-2023 10:24:21

1K+ Views

To delete the prefix substring from the given string first, we check if the given substring is present in the specified string or not using the inbuilt hasPrefix() function. Then find the index of the prefix substring using the inbuilt index() function and finally remove the prefix substring. Input String = “Today is cloudy day” SubString = “Today” Output “is cloudy day” Here, the specified substring is found in the given string, so in the resultant string, we removed the substring from the start of the input string. Algorithm Step 1 − Create a string. Step 2 ... Read More

Swift Program to Create an Empty Dictionary

Ankita Saini
Updated on 09-May-2023 10:21:44

850 Views

A dictionary is an unordered collection in which data is stored in the form of key-value pairs, where keys are the unique identifiers of any data type like string, integer, etc., that are connected with each value. In Swift, we are allowed to create an empty dictionary. An empty dictionary is a dictionary which does not contain any element or we can say that its size is zero. Syntax let myDicti = [Int:Int]() Or let myDicti:[Int:Int]= [:] Or let myDicti:[String:Int]= [:] So to create an empty dictionary you can use any of the given syntaxes. Here it ... Read More

Swift Program to count a specified character inside the string

Ankita Saini
Updated on 09-May-2023 10:18:20

2K+ Views

In Swift, to count how many times a specified character occurs in the given string we create a function. It will increase the value of the counter by one whenever the specified character appears in the given string and then return the final count at the end of the given string. Input String = “Today is cloudy day” Character = “y” Output 3 Where, character “y” appears three times in the given string so the output of the input is 3. Algorithm Step 1 − Create a function that returns the specified character's total occurrence. Step 2 ... Read More

Swift Program to convert the array of characters into the string

Ankita Saini
Updated on 09-May-2023 10:08:02

1K+ Views

To convert the array of characters into a string Swift provides the following methods − Using String() initialiser Using append() method Input array = [“S”, “w”, “i”, “f”, “t”] Output Swift Here we join all the characters given in the array to create a string. Method 1: Using String() Initialiser To convert the array of characters into a string we can use String() initialisers. The String() initialiser will create the object of a string. Here we use parameterise string initialiser. Syntax Swift(value) Here value represents the array which we want to convert into a string. ... Read More

Swift Program to check the given string is empty or not

Ankita Saini
Updated on 09-May-2023 10:03:05

3K+ Views

To check if the given string is empty or not Swift provides an inbuilt property named as isEmpty. This property will return true if the given string is empty. And return false if the given string is not empty. Here empty string means whose length is 0 or we can say a string that does not contain any character. String 1 = “Swift” String 2 = “” Where string 1 is not an empty string because it contains a sequence of characters or it has some length. Whereas string 2 is an empty string because it does contains anything ... Read More

Swift Program to Check if two Dictionaries are equal

Ankita Saini
Updated on 09-May-2023 09:59:28

909 Views

In Swift, a dictionary is an unordered collection in which data is stored in the form of key-value pairs, where keys are the unique identifiers. So to check the equality of two dictionaries we first need to check if the size of both the dictionaries is equal or not. If yes, then we check if both dictionaries contain the same key-value pairs or not. If both conditions are equal, then that means the given two dictionaries are equal. If any of the conditions are false then that means the given dictionaries are not equal. Algorithm Step 1 − Create ... Read More

Advertisements