About 1,240,000 results
Open links in new tab
  1. Python Assign String Variables - W3Schools

    Assign String to a Variable. Assigning a string to a variable is done with the variable name followed by an equal sign and the string:

  2. Initialize a string variable in Python: "" or None?

    But also for strings, a (empty) string object is instanciated at runtime for the keyword parameter. The cleanest way is probably: def myfunc(self, my_string=None): self.my_string = my_string or "" # or a if-else-branch, ...

  3. python - Insert variable values into a string - Stack Overflow

    I want to introduce a variable [i] into a string in Python. For example look at the following script. I just want to be able to give a name to the image, for example geo[0].tif ... to geo[i].tif, or if you use an accountant as I can replace a portion of the value chain to generate a counter.

  4. Convert String into Variable Name in Python - GeeksforGeeks

    Sep 25, 2023 · While Python does not directly allow you to convert a string into a variable name, these examples demonstrate various approaches to achieve similar functionality using dictionaries, functions, exec(), or custom classes.

  5. Insert a Variable into a StringPython | GeeksforGeeks

    Nov 20, 2024 · Let’s look at some simple methods to insert variables into strings. The simplest way to insert a variable into a string is by using the + operator. This method combines strings together. Here's an example: Other in built functions we can use are: Python’s format () method is another easy way to insert variables into a string.

  6. Python: Using vars () to assign a string to a variable

    Jul 9, 2015 · vars() (or locals()) provides low level access to variables created by python. Thus the following two lines are equivalent. locals()['x'] = 4 x = 4 The scope of vars()['x'] is exactly the same as the scope of x.

  7. How To Insert Variable Into String In Python? (6 Methods + Code)

    One common task in Python programming is inserting a variable into a string. Whether you need to display dynamic information or construct complex output, knowing how to incorporate variables into strings is crucial. In this guide, we will explore different approaches to achieve this in Python, along with examples and best practices.

  8. Text and String Variables in Python - Dive Into Python

    May 3, 2024 · You can declare a string variable by assigning a string value to a variable name using the equals sign =. Here's an example: my_string = "Hello, world!" In this example, we have declared a string variable called my_string and assigned it the value "Hello, world!". Note that the string value is enclosed in quotation marks.

  9. Python: Add Variable to String & Print Using 4 Methods

    Sep 5, 2023 · The str.format() method in Python is used for string formatting and is an alternative to the older % operator for string interpolation. The following example shows how to use The str.format() method to add variables to a string; # Variables name = "Pytutorial" age = 22 # Insert Variables into String using .format() formatted_string = "Hello, {}!

  10. Assigning Values to Variables - GeeksforGeeks

    Jul 4, 2024 · Below are the steps and methods by which we can assign values to variables in Python and other languages: Direct Initialization Method Using Conditional Operator

  11. Some results have been removed
Refresh