The modification of strings in Python and the detailed _python

Source: Internet
Author: User

Find problems

Recently encountered a topic in the interview, choose to use JavaScript or Python to implement string inversion, 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

The interviewer then asked two questions:

(1) Can you modify the value of a string like this? "I can answer," "answer the wrong."

(2) The incoming parameter is an address? Or a copy? "I answered, passing the value. Numbers, strings, tuple values (immutable), list and dict references (mutable), "" "Answer the value, you can directly modify the" "answer the wrong, correct is the value, immutable"

Consider the following

Although the string is often used, but has not really studied the problem, so the Internet search information:

Strings in Python are immutable types, which means that elements that change a string need to create a new string.

A string is composed of independent characters and is also a sequence, and the common method of operation of the sequence also applies to strings.

For example:

Accessing substrings sequentially by slicing operations;

The length of the string is obtained by Len ().

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

Python does not have the type of characters, but instead uses a string of length 1 to represent the concept, which is, of course, a substring.

Access String Examples:

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 it to a variable (or by assigning a value). 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)

Output:

Hello python!
Different string altogether

So how do you delete a character or string?

Again, the string is immutable, so you can't just delete a character from a string, you can either empty an empty string, or combine strings that strip out unwanted parts to form a new string.

Suppose you want to get from "Hello world!" Inside to delete lowercase "l", then 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 by using the DEL statement. However, in most applications, it is not necessary to explicitly delete a string. The code that defines this string ends when Python automatically releases the strings.

So, the inverse string code I wrote is problematic, and 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 incoming formal parameter s and the argument s the same object? You can use the ID function to verify, first look at the official explanation of the ID function.

That is, the id(obj) function returns the address of the object obj in memory during its lifecycle, and the parameter type of the ID function is an object (all objects in Python, references to objects in a variable)

We can use the code below 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)

Output:

38264224
38264224
Dcfdsafdsabadcfdsafdsaba

You can see that the incoming parameter is actually the address of the string object, and if you change the argument to list or dict, the output ID is the same, so the way to get the parameters in Python is the address of the incoming object, except that numbers, strings and tuple are not directly modified, and the list and dict can be directly modified.

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.