mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-24 01:51:38 +00:00
Merge pull request #3802 from mxschumacher/update-string-formatting
python3/en/ Update string formatting
This commit is contained in:
commit
6565524006
@ -16,8 +16,6 @@ Python was created by Guido van Rossum in the early 90s. It is now one of the mo
|
|||||||
languages in existence. I fell in love with Python for its syntactic clarity. It's basically
|
languages in existence. I fell in love with Python for its syntactic clarity. It's basically
|
||||||
executable pseudocode.
|
executable pseudocode.
|
||||||
|
|
||||||
Feedback would be highly appreciated! You can reach me at [@louiedinh](http://twitter.com/louiedinh) or louiedinh [at] [google's email service]
|
|
||||||
|
|
||||||
Note: This article applies to Python 3 specifically. Check out [here](http://learnxinyminutes.com/docs/python/) if you want to learn the old Python 2.7
|
Note: This article applies to Python 3 specifically. Check out [here](http://learnxinyminutes.com/docs/python/) if you want to learn the old Python 2.7
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@ -139,20 +137,6 @@ b == a # => True, a's and b's objects are equal
|
|||||||
# You can find the length of a string
|
# You can find the length of a string
|
||||||
len("This is a string") # => 16
|
len("This is a string") # => 16
|
||||||
|
|
||||||
# .format can be used to format strings, like this:
|
|
||||||
"{} can be {}".format("Strings", "interpolated") # => "Strings can be interpolated"
|
|
||||||
|
|
||||||
# You can repeat the formatting arguments to save some typing.
|
|
||||||
"{0} be nimble, {0} be quick, {0} jump over the {1}".format("Jack", "candle stick")
|
|
||||||
# => "Jack be nimble, Jack be quick, Jack jump over the candle stick"
|
|
||||||
|
|
||||||
# You can use keywords if you don't want to count.
|
|
||||||
"{name} wants to eat {food}".format(name="Bob", food="lasagna") # => "Bob wants to eat lasagna"
|
|
||||||
|
|
||||||
# If your Python 3 code also needs to run on Python 2.5 and below, you can also
|
|
||||||
# still use the old style of formatting:
|
|
||||||
"%s can be %s the %s way" % ("Strings", "interpolated", "old") # => "Strings can be interpolated the old way"
|
|
||||||
|
|
||||||
# You can also format using f-strings or formatted string literals (in Python 3.6+)
|
# You can also format using f-strings or formatted string literals (in Python 3.6+)
|
||||||
name = "Reiko"
|
name = "Reiko"
|
||||||
f"She said her name is {name}." # => "She said her name is Reiko"
|
f"She said her name is {name}." # => "She said her name is Reiko"
|
||||||
|
Loading…
Reference in New Issue
Block a user