Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PyCharm to solve the problem of string disappearing strangely

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about how to use PyCharm to solve the problem of strange disappearance of strings. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

In the exchange group today, someone asked a question like this:

The images involved in the screenshot are:

At first, I thought it was a simple question, but then I found out it wasn't that simple.

I want to come to this student's code and run it in debug mode in PyCharm. The effect is as follows:

As of the penultimate line, the data in the con string is normal. But once you print it out using the print function, the data is wrong again.

To illustrate this problem, I'll write a piece of code to briefly reproduce the problem.

As you can see from the screenshot, an is a normal list, not a generator. When we directly for loop to expand the a list, and then print the data inside, it can be displayed normally. But when I use'. Join (a) in advance to concatenate the strings in the a list into a string, I find that only the last string in the a list can be displayed. The first two strings mysteriously disappeared.

To explain why this problem occurs, let's take a look at what the a list looks like:

As you can see, there is a\ r at the end of the first two elements in the a list. Let's take a look at what the b string generated using'. Join (a) looks like:

Note that you need to enter the name of the string directly to show what it really looks like, and you cannot print it using the print function. The difference between the two, you can see my article: [one technology a day] uncover the two "faces" of the string.

Can you see that? In fact, when we execute'. Join (a), it correctly splices the three strings in the a list, and the result is abc\ rdef\ rxyz\ r. But the bad is bad here. This symbol is not a tab here, but rather tells the output cursor, now move to the beginning of the line.

How to understand this phenomenon, let's assume that there are nine buckets numbered 1-9 and nine balls numbered 1-9. Under normal circumstances, put the No. 1 ball into the No. 1 barrel, the No. 2 ball into the No. 2 barrel, and the No. 3 ball into the No. 3 bucket. Put the No. 9 ball in the No. 9 barrel. This is our normal printing process.

But now, we add a special ball between balls 3-4, which says\ r. Add a\ r ball between numbers 6 and 7. First of all, put the No. 1 ball into the No. 1 bucket, the No. 2 ball into the No. 2 bucket, and the No. 3 ball into the No. 3. Then, you see the ball, which is not in the bucket, but requires you to go back to barrel 1, put the ball in bucket 1, put the ball in barrel 1, put the ball in barrel 2, and put the ball in barrel 3. Next, you meet the ball, you go back to barrel 1, put the ball 7 into barrel 1, the ball 8 into barrel 2, and the ball 9 into 3. After some operation, in all the buckets, the top ball is only 7, 8, and 9. The other balls 1-6 are covered.

Note that here I use the ball in the bucket as an example, rather than Word typing, because when we use Word, if you move the cursor back to the beginning of the line to enter new characters, the existing characters will fall back in order. But what we're dealing with today is covering it sequentially. So don't confuse the two situations.

To prove this sequential overlay, let's take a look at the following example:

Please remember the example of putting a ball in the bucket above.\ r the ball is not put in the bucket, it tells you to go back to the beginning of the line. So, our first string is 11111111111, and because it is followed by a\ r, we now go back to the beginning of the line and continue to output 22222. There are five 2s, so the string becomes 22222111111. Then I encountered another\ r, so I went back to the beginning of the line and output 333. So the final result is 33322111111.

The crux of this problem is\ r, not what's wrong with'. Join (). You can even manually enter a string with\ r, and the effect is the same:

It is also important to note that this\ r only affects the printout of the print function. However, the string itself does not appear\ r followed by the character overwriting the previous character. So, for a string aaaaa\ rbb, we can see that although print prints out only bbaaa5 characters, it actually has eight characters:

After reading the above, do you have any further understanding of how to use PyCharm to solve the problem of strange disappearance of strings? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report