Found 10784 Articles for Python

How to remove a key from a python dictionary?

Pranathi M
Updated on 16-Sep-2022 06:49:27

730 Views

In python, a dictionary is an unordered collection of data which is used to store data values such as map unlike other datatypes that store only single values. Keys of a dictionary must be unique and of immutable data type such as Strings, Integers, and tuples, but the key values can be repeated and be of any type. Dictionaries are mutable, therefore keys can be added or removed even after defining a dictionary in python. They are many ways to remove a key from a dictionary, following are few ways. Using pop(key, d) The pop(key, d) method returns the value ... Read More

How to add new keys to a dictionary in Python?

Pythonic
Updated on 30-Jul-2019 22:30:20

762 Views

Dictionary is an unordered collection of key-value pairs. Each element is not identified by positional index. Moreover, the fact that key can’t be repeated, we simply use a new key and assign a value to it so that a new pair will be added to dictionary. >>> D1 = {1: 'a', 2: 'b', 3: 'c', 'x': 1, 'y': 2, 'z': 3} >>> D1[10] = 'z' >>> D1 {1: 'a', 2: 'b', 3: 'c', 'x': 1, 'y': 2, 'z': 3, 10: 'z'}

How to merge two Python dictionaries in a single expression?

Pythonic
Updated on 30-Jul-2019 22:30:20

110 Views

Built-in dictionary class has update() method which merges elements of argument dictionary object with calling dictionary object. >>> a = {1:'a', 2:'b', 3:'c'} >>> b = {'x':1,'y':2, 'z':3} >>> a.update(b) >>> a {1: 'a', 2: 'b', 3: 'c', 'x': 1, 'y': 2, 'z': 3} From Python 3.5 onwards, another syntax to merge two dictionaries is available >>> a = {1:'a', 2:'b', 3:'c'} >>> b = {'x':1,'y':2, 'z':3} >>> c = {**a, **b} >>> c {1: 'a', 2: 'b', 3: 'c', 'x': 1, 'y': 2, 'z': 3}

How to change any data type into a string in Python?

Pythonic
Updated on 30-Jul-2019 22:30:20

177 Views

Any built-in data type converted into its string representation by str() function >>> str(10) '10' >>> str(11.11) '11.11' >>> str(3+4j) '(3+4j)' >>> str([1,2,3]) '[1, 2, 3]' >>> str((1,2,3)) '(1, 2, 3)' >>> str({1:11, 2:22, 3:33}) '{1: 11, 2: 22, 3: 33}' For a user defined class to be converted to string representation, __str__() function needs to be defined in it. >>> class rectangle: def __init__(self): self.l=10 self.b=10 def __str__(self): return 'length={} breadth={}'.format(self.l, self.b) >>> r1=rect() >>> str(r1) 'length = 10 breadth = 10'

What are different data conversion methods in Python?

Sarika Singh
Updated on 14-Nov-2022 08:22:29

615 Views

Type conversion is the transformation of a Python data type into another data type. Implicit type conversion and explicit type conversion are the two basic categories of type conversion procedures in Python. We will cover the following topics in this article − Implicit Type Conversion in Python is carried out by the Python interpreter automatically. In Python, explicit type conversion must be performed directly by the programmer. Let's study more about these two approaches in depth and with some illustrations. Implicit Type conversions Implicit type conversion occurs when the Python interpreter automatically changes an object's data type without ... Read More

How to print concatenated string in Python?

Pythonic
Updated on 30-Jul-2019 22:30:20

341 Views

When used with strings, plus (+) is defined as concatenation operator. It appends second string to the first string. >>> s1 = 'TutorialsPoint ' >>> s2 = 'Hyderabad' >>> print (s1+s2) TutorialsPoint Hyderabad

How to print a string two times with single statement in Python?

Pythonic
Updated on 30-Jul-2019 22:30:20

172 Views

When used with strings, asterisk (*) is defined as repetition operator. It concatenates given string as many times as number followed by asterisk. >>> string = 'abcdefghij' >>> print (string*2) abcdefghijabcdefghij

How to divide a string by line break or period with Python regular expressions?

Rajendra Dharmkar
Updated on 16-Dec-2019 08:57:39

297 Views

The following code splits given string by a period and a line break as followsExampleimport re s = """Hi. It's nice meeting you. My name is Jason.""" result = re.findall(r'[^\s\.][^\.]+', s) print resultOutputThis gives the following output['Hi', "It's nice meeting you", 'My name is Jason']

How can I match the start and end in Python's regex?

Rajendra Dharmkar
Updated on 08-Sep-2023 13:51:19

627 Views

Have you ever come across situations where you had to determine if a string starts or ends with a particular pattern in Python? If so, you don’t need to worry, for Python's regular expressions provide a solution in such cases. With the power of regex, you can easily check whether a string begins or concludes with a specific sequence of characters. In this comprehensive article, we will explore various code examples that demonstrate how to use Python's regex to accomplish this task effortlessly. Before taking a plunge into the examples, let's take a break and understand what regular expressions are ... Read More

How do I do a case-insensitive string comparison in Python?

Rajendra Dharmkar
Updated on 08-Sep-2023 13:58:17

1K+ Views

Have you ever encountered a situation where you needed to compare strings in Python but wanted to ignore the differences in letter casing? Worry not, for Python provides straightforward solutions to achieve case−insensitive string comparisons. In this comprehensive guide, we will explore five different code examples, each showcasing a unique method for conducting case−insensitive string comparisons. So, let's dive in and discover the wonders of Python's string comparison versatility! Before we delve into the examples, let's briefly understand what a case−insensitive string comparison entails. When comparing strings, a case−insensitive approach treats uppercase and lowercase letters as equal, thereby disregarding the ... Read More

Advertisements