- Learn Programming in Python with Cody Jackson
- Cody Jackson
- 332字
- 2021-06-10 19:06:08
Strings
Strings in programming are simply text; either individual characters, words, phrases, or complete sentences. They are one of the most common elements to use when programming, at least when it comes to interacting with the user. Because they are so common, they are a native data type within Python, meaning they have many powerful capabilities built in. Unlike other languages, you don't have to worry about creating these capabilities yourself.
Strings in Python are different than in most other languages. First off, there are no char types, only single character strings (char types are single characters, separate from actual strings, used for memory conservation). Strings also can't be changed in-place; a new string object is created whenever you want to make changes to it, such as concatenation. This means you have to be aware that you are not manipulating the string in memory; it doesn't get changed or deleted as you work with it. You are simply creating a new string each time.
Empty strings are written as two quotes with nothing in between. The quotes used can be either single or double; my preference is to use double quotes, since you don't have to escape the single quote to use it in a string. That means you can write a statement like the following:
"And then he said, 'No way', when I told him."
If you want to use just one type of quote mark all the time, you have to use the backslash character to escape the desired quote marks so Python doesn't think it's at the end of the phrase, like this:
"And then he said, \"No way\", when I told him."
Triple quoted blocks are for strings that span multiple lines, as shown in Chapter 1, The Fundamentals of Python. Python collects the entire text block into a single string with embedded newline characters. This is good for things like writing short paragraphs of text; for example, instructions, or for formatting your source code for clarification.
- Bootstrap Site Blueprints Volume II
- 深入淺出Java虛擬機:JVM原理與實戰
- Interactive Data Visualization with Python
- Django開發從入門到實踐
- Offer來了:Java面試核心知識點精講(原理篇)
- 深入淺出Windows API程序設計:編程基礎篇
- Swift 3 New Features
- Python 3破冰人工智能:從入門到實戰
- TradeStation交易應用實踐:量化方法構建贏家策略(原書第2版)
- 大話Java:程序設計從入門到精通
- MATLAB GUI純代碼編寫從入門到實戰
- Mastering Docker
- Natural Language Processing with Python Quick Start Guide
- MySQL 8從零開始學(視頻教學版)
- Machine Learning for OpenCV