For details about modifying and passing parameters of strings in Python, python

Source: Internet
Author: User

For details about modifying and passing parameters of strings in Python, python

Problems Found

Recently I encountered a question during the interview. I used JavaScript or Python to reverse the string. I chose Python and wrote the code (incorrect ):

#!/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 I modify the value of a string like this? [My answer, yes] [incorrect answer]

(2) is the input parameter an address? Or a copy? [In my answer, pass the value. Number, string, and immutable; list and dict are mutable;] [Answer To pass value, you can directly modify] [Answer error, correct is to pass value, immutable]

Consider the following:

Although the string is often used, but I have never studied this problem, so I searched the internet for information:

The string type in Python is immutable, that is, to change the element of a string, you need to create a new string.

A string is composed of independent characters and also a sequence. The general operation method of the sequence is also applicable to strings.

For example:

Access the sub-string sequentially through the slice operation;

Evaluate the length of a string using len;

Use the in or not in operator to determine whether a character exists in the string.

Python does not have the character type, but uses a string with a length of 1 to express this concept. Of course, this is actually a substring.

Example of an access string:

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

Output:

HelloWorld!

So how to change a string?

You can "Update" an existing string by assigning (or re-assigning) a value to a variable. The new value may be similar to the original value or 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)

Output:

Hello Python!different string altogether

How can I delete a character or string?

Repeat it again. The string is unchangeable, so you cannot just delete a character in a string. What you can do is to clear an empty string, or combine the strings after removing unnecessary parts to form a new string.

Assume that you want !" To delete the lower-case "l", you need to do this:

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

Output:

Helo World!

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, and then Python will automatically release these strings.

Therefore, the reverse string code I wrote is problematic. 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 ts = 'abcd'print reverse(s)

Is the input parameter s the same object as the real parameter s? You can use the id function for verification. First, let's look at the official explanation of the id function.

That is to say,id(obj)The function returns the address of the object obj in the memory within its lifecycle. The parameter type of the id function is an object (all objects in Python are stored in the variables as references of objects)

We can use the following code for verification:

#!/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 ts = 'abasdfasdfcdabasdfasdfcd'print id(s)print reverse(s)

Output:

3826422438264224dcfdsafdsabadcfdsafdsaba

It can be seen that the input parameter is actually the address of the string object. If you replace the parameter with list or dict, the output id is the same. Therefore, in Python, parameters are transmitted to the object address, but numbers, strings, and tuple cannot be directly modified, while list and dict can be directly modified.

Summary

The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.