Python Forum
Can you explain the strings in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can you explain the strings in Python
#1
x = 2
x = str(x)
print("Years on the force is "  + x)
Years on the force is 2 #Casting. 2 is not string but a int. Python sees this as a string or int? Never mind. I got it. x = str(2) is a conversion to string from an integer. So you put a string on the inside of print.
Programs are like instructions or rules. Learning it gets us closer to a solution. Desired outcome. Computer talk.
Reply
#2
str(x) is not a cast. str(x) creates a new str object which is the string representation of the value of x.

x is a variable. x is never an int or a str, it is a name that is used to reference different objects. Kind of like a key in a dictionary. In fact, "x" is a key in a dictionary you can reference by calling locals().
x = 2
print(locals()["x"], id(x), type(x))
x = str(x)
print(locals()["x"], id(x), type(x))
Output:
2 140726717440840 <class 'int'> 2 2176490884464 <class 'str'>
id() prints the ID of an object. id(x) prints the ID of an object referenced by "x".

The first print shows that x references an int object. The second print shows x references a str object. The two objects have different ID's, proving they are completely different objects.

The
Reply
#3
Thanks. I get the idea. Those are the in built methods. locals()
Quote:Years on the force is 15
2 138810210880456 <class 'int'>
2 138810198583792 <class 'str'>

=== Code Execution Successful ===
Programs are like instructions or rules. Learning it gets us closer to a solution. Desired outcome. Computer talk.
Reply
#4
Just use f-string, no need for str()

x = 2
print(f"Years on the force are {x}")
You can even put a function in the curly brackets!

x = 2
y = 2
def add(a, b):
    return a+b

print(f"Years on the force are {add(x, y)}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 934 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  [split] Explain the python code in this definition Led_Zeppelin 1 834 Jan-13-2023, 10:20 PM
Last Post: deanhystad
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,150 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  Explain the python code in this definition Led_Zeppelin 1 1,205 Oct-27-2022, 04:04 AM
Last Post: deanhystad
  Sudoku Solver in Python - Can someone explain this code ? qwemx 6 2,322 Jun-27-2022, 12:46 PM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,918 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,636 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  Python: if 'X' in 'Y' but with two similar strings as 'X' DreamingInsanity 6 4,037 Feb-01-2019, 01:28 PM
Last Post: buran
  Splitting strings in python? NLittle17 3 2,512 Jan-05-2019, 09:20 AM
Last Post: Axel_Erfurt
  lists, strings, and byte strings Skaperen 2 4,358 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020