Detailed explanation of string modification and parameter passing in Python

WBOY
Release: 2016-12-05 13:27:17
Original
1510 people have browsed it

Problem found

I recently encountered a question during an interview. I chose to use JavaScript or Python to reverse strings. I chose Python and then wrote the code (wrong):

#!/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
Copy after login

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 parameter passed in an address? Or a copy? [I answered, pass value. Numbers, strings, and tuples are passed by value (immutable); list and dict are passed by reference (mutable);] [Answer is passed by value, which can be modified directly] [Answer is wrong, the correct one is pass by value, immutable]

Thinking about the following

Although I often use strings, I have never studied this issue, so I searched the Internet for information:

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

A string is composed of independent characters and is also a sequence. The general operation methods of sequences also apply to strings.

For example:

Access substrings sequentially through slicing operations;

Use len() to find the length of a string, etc.;

​ Determine whether a certain character exists in the string through the in or not in operator.

There is no character type in Python, but a string of length 1 is used to represent this concept. Of course, this is actually a substring.

Example of access string:

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

Output:

H
ello
World!
Copy after login

So how to 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.

For example:

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

Output:

Hello Python!
different string altogether
Copy after login

So how to delete a character or string?

Repeat again, strings are immutable, so you can’t just delete a certain character in a string. What you can do is to clear an empty string, or combine strings with unnecessary parts removed. rise up 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)
Copy after login

Output:

Helo World!
Copy after login

Empty or delete a string by assigning an empty string or using the del statement. However, in most applications, there is no need to explicitly delete strings. The code that defines this string will eventually end, at which time Python will automatically release the string.

So, there is something wrong with the reverse string code I wrote. 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)
Copy after login

So are the passed formal parameter s and actual parameter s the same object? You can use the id function to verify. Let's first look at the official explanation of the id function.

In other words, the id(obj) function returns the address of the object obj in memory during its life cycle. The parameter type of the id function is an object (for all objects in Python, the variable stores a reference to the object)

We can use the following code to verify:

#!/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)
Copy after login

Output:

38264224
38264224
dcfdsafdsabadcfdsafdsaba
Copy after login

It can be seen that the parameter passed in is actually the address of the string object. If the parameter is changed to list or dict, the output id will still be the same. Therefore, the method of passing parameters in Python is to pass the address of the object , but numbers, strings and tuples cannot be modified directly, but list and dict can be modified directly.

Summary

The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message to communicate.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!