Strings are an essential part of JavaScript, used for everything from simple text messages to complex HTML templates. One of the most user-friendly ways to work with strings in JavaScript is by using template literals. In this article, we’ll explore what template literals are and how they can make your code cleaner and more readable.
Continue readingTag: string (Page 1 of 2)
In the vast landscape of Python’s standard library, there’s a hidden gem that can significantly simplify the task of counting occurrences within an iterable. Say hello to the collections.Counter
class, a versatile tool that effortlessly tallies the frequency of elements in your data. In this brief blog article, we’ll take a closer look at how you can harness the power of collections.Counter
to elegantly count the occurrences of characters in a string.
Strings are a fundamental data type in Python, forming the building blocks for text manipulation and processing. However, it’s crucial to understand that strings in Python are immutable. This means that once a string is created, it cannot be modified in-place. Attempting to directly modify a string will result in the creation of a new string object. In this article, we’ll explore the concept of string immutability, shed light on its implications, and emphasize the importance of utilizing appropriate string manipulation methods.
Continue readingOne of the essential features of any programming language is the ability to manipulate strings. In this article, we’ll explore a simple yet powerful Python string method called title()
. We will see how to use this method to capitalize the first letter of each word in a given string.
The title()
method is a built-in Python string method that capitalizes the first letter of each word in a given string. It returns a new string where the first letter of each word is capitalized, and all other letters are in lowercase. Here is the syntax of the title()
method:
string.title()
Continue reading
In Python, there are many useful tricks and techniques that can help you write cleaner, more efficient code. One such trick is using the join()
method to create comma-separated strings from lists of strings. This technique can be particularly useful when you need to output a list of items in a human-readable format.
In Python, it’s possible to perform mathematical operations inside of string literals, using a feature called “f-strings”. F-strings, or “formatted string literals”, allow you to embed expressions inside string literals, which are evaluated at runtime. This allows you to mix variables, arithmetic expressions, and other computations into strings in a very readable and convenient way.
num_val = 42
print(f'{num_val % 2 = }') # num_val % 2 = 0
Here, the f-string '{num_val % 2 = }'
is a string literal that contains an expression num_val % 2
. The %
operator is used to perform the modulo operation, which returns the remainder of the division of one number by another. In this case, num_val % 2
returns the remainder of the division of 42
by 2
, which is 0
.
Anagrams are strings that have the same letters but in a different order for example abc
, bca
, cab
, acb
, bac
are all anagrams, since they all contain the same letters.
We can check whether two strings are anagrams in Python in different ways. One way to do that would be to use Counter
from the collections
module.
From the documentation:
A Counter is a dict subclass for counting hashable objects. It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.
In plain English, with Counter
, we can get a dictionary that represents the frequency of elements in a list. Let us see this in an example:
from collections import Counter
print(Counter("Hello")) # Counter({'l': 2, 'H': 1, 'e': 1, 'o': 1})
Now we can use Counter
to quickly check whether two strings are anagrams or not:
from collections import Counter
def check_if_anagram(first_string, second_string):
first_string = first_string.lower()
second_string = second_string.lower()
return Counter(first_string) == Counter(second_string)
print(check_if_anagram('testinG', 'Testing')) # True
print(check_if_anagram('Here', 'Rehe')) # True
print(check_if_anagram('Know', 'Now')) # False
We can also check whether 2 strings are anagrams using sorted():
def check_if_anagram(first_word, second_word):
first_word = first_word.lower()
second_word = second_word.lower()
return sorted(first_word) == sorted(second_word)
print(check_if_anagram("testinG", "Testing")) # True
print(check_if_anagram("Here", "Rehe")) # True
print(check_if_anagram("Know", "Now")) # False
That’s basically it.
I hope you find this useful.
One way of introducing potential bugs is the lack of information about the way scopes work inside functions in Python.
When we declare global variables, their value may not change as we may expect.
Let us see this in an example to understand how a simple example can be misleading and confusing for a lot of people.
Continue readingYou may need to read text files and do some formatting of those strings.
With formatting, it means that you may also need to replace a certain portion of the string with another string.
Let us assume that we have a sentence and want to replace all the occurrences of the word i with I.
Continue readingA palindrome is a word that is the same whether you read it forwards or backward.
Checking whether an input string is a palindrome can be a common interview question that you may encounter at least in one of the technical rounds of your interviews.
Continue reading