SoFunction
Updated on 2024-10-30

String Modification and Passing Parameters in Python Explained

Problems found

I recently came across a question in an interview where I chose to implement string reversal in JavaScript or Python, I chose Python and then wrote the code (incorrectly):

#!/usr/bin/env python
#-*-coding:utf-8-*-
__author__ = 'ZhangHe'
def reverse(s):
 l = 0
 r = len(s) - 1
 while l < r:
  s[l],s[r] = s[r],s[l]
  l += 1
  r -= 1
 return s

Then the interviewer asked two questions:

(1) Can the value of a string be modified in this way? [I answered, yes.] [Wrong answer]

(2) Is the incoming parameter an address? Or a copy? [I answered pass value. Numbers, strings, tuples pass values (immutable); lists and dicts pass references (mutable);] [answered pass values, can be directly modified] [answered incorrectly, the correct is to pass values, immutable]

Think about the following

Although I use strings a lot, I hadn't really looked into this, so I googled for information:

Strings in Python are immutable types, meaning that changing the elements of a string requires creating a new string.

A string is made up of individual characters and is also a sequence, and the generalized methods of operation for sequences apply to strings as well.

Example:

Substrings are accessed sequentially by slicing operations;

Find the length of a string by len(), etc;

Determines whether a character exists in a string by using the in or not in operator.

Python doesn't have a character type, but instead represents the concept as a string of length 1, which is, of course, actually a substring.

Example of accessing a string:

1 aString = 'Hello World!'
2 print(aString[0])
3 print(aString[1:5])
4 print(aString[6:])

Output:

H
ello
World!

So how do you change a string?

You can "update" an existing string by assigning (or reassigning) a value to a variable. The new value may be similar to the original value, or it may be completely different from the original string.

Example:

1 aString = 'Hello World!'
2 aString = aString[:6] + 'Python!'
3 print(aString)
4 aString = 'different string altogether'
5 print(aString)

Output:

Hello Python!
different string altogether

So how do you delete a character or string?

To repeat, strings are immutable, so you can't just delete a character from a string; what you can do is empty an empty string, or combine strings after eliminating unwanted parts to form a new string.

Suppose you want to remove the lowercase "l" from "Hello World!", then you need to do this:

1 aString = 'Hello World!'
2 aString = aString[:3] + aString[4:]
3 print(aString)

Output:

Helo World!

A string can be cleared or deleted by assigning it an empty string or by using the del statement. However, in most applications, there is no need to explicitly delete strings. The code that defines the string will eventually end, and Python will free the string automatically.

So, there is something wrong with the code I wrote to reverse the string, the correct code should be:

#!/usr/bin/env python
#-*-coding:utf-8-*-
__author__ = 'ZhangHe'
def reverse(s):
 t = ''
 r = len(s) - 1
 while r>=0:
  t = t + s[r]
  r -= 1
 return t
s = 'abcd'
print reverse(s)

So is the passed-in formal parameter s and real parameter s the same object or not? You can use the id function to verify this. First, let's look at the official explanation of the id function.

In other words.id(obj)The function returns the address of the object obj located in memory during its lifetime, the type of the argument to the id function is an object (everything is an object in Python, variables hold references to objects)

We can verify this with the code below:

#!/usr/bin/env python
#-*-coding:utf-8-*-
__author__ = 'ZhangHe'
def reverse(s):
 print id(s)
 t = ''
 r = len(s) - 1
 while r>=0:
  t = t + s[r]
  r -= 1
 return t
s = 'abasdfasdfcdabasdfasdfcd'
print id(s)
print reverse(s)

Output:

38264224
38264224
dcfdsafdsabadcfdsafdsaba

You can see that the incoming parameter is actually the address of the string object, if the parameter is replaced by a list or a dict, then the output id is still the same, so all, Python in the way to pass the parameter is to pass the address of the object, only the number of strings and tuples can not be directly modified, while the list and the dict can be directly modified.

summarize

The above is the entire content of this article, I hope the content of this article on your learning or work can bring some help, if there are questions you can leave a message to exchange.