[轉載] 字符串太長 pep8_Python f字符串– PEP 498 –文字字符串插值

參考鏈接: 從Java中的字符串中刪除前導零

字符串太長 pep8

?

??

??

?

?Python f-strings or formatted strings are the new way to format strings. This feature was introduced in Python 3.6 under PEP-498. It’s also called literal string interpolation.

? Python f字符串或格式化的字符串是格式化字符串的新方法。 此功能是在PEP-498下的Python 3.6中引入的。 也稱為文字字符串插值 。??

? 為什么我們需要F弦? (Why do we need f-strings?)?

?Python provides various ways to format a string. Let’s quickly look at them and what are the issues they have.

? Python提供了多種格式化字符串的方法。 讓我們快速查看它們以及它們有什么問題。??

?

??

?% formatting – great for simple formatting but limited support for strings, ints, doubles only. We can’t use it with objects. %格式 -非常適合簡單的格式設置,但僅對string ,ints和double的支持有限。 我們不能將其與對象一起使用。 Template Strings – it’s very basic. Template strings work with keyword arguments like dictionary only. We are not allowed to call any function and arguments must be strings. 模板字符串 –非常基本。 模板字符串僅可與關鍵字參數(例如字典)一起使用。 我們不允許調用任何函數,并且參數必須是字符串。 String format() – Python String format() function was introduced to overcome the issues and limited features of %-formatting and template strings. However, it’s too verbose. Let’s look at its verbosity with a simple example.>>> age = 4 * 10

>>> 'My age is {age}.'.format(age=age)

'My age is 40.' 字符串格式()– Python字符串格式()函數的引入是為了克服%格式和模板字符串的問題和有限的功能。 但是,它太冗長了。 讓我們用一個簡單的例子來看看它的詳細程度。?

?Python f-strings works almost similar like format() function but removes all the verbosity that format() function has. Let’s see how easily we can format the above string using f-strings.

? Python f字符串的工作原理幾乎類似于format()函數,但刪除了format()函數具有的所有冗長性。 讓我們看看使用f字符串格式化上述字符串的難易程度。??

?>>> f'My age is {age}'

'My age is 40.'

?Python f-strings is introduced to have minimal syntax for string formatting. The expressions are evaluated at runtime. If you are using Python 3.6 or higher version, you should use f-strings for all your string formatting requirements.

? 引入Python f字符串是為了使字符串格式化的語法最少 。 在運行時對表達式求值。 如果您使用的是Python 3.6或更高版本,則應使用f-strings滿足所有字符串格式要求。??

? Python f字符串示例 (Python f-strings examples)?

?Let’s look at a simple example of f-strings.

? 讓我們看一個簡單的f字符串示例。??

?name = 'Pankaj'

age = 34

?

f_string = f'My Name is {name} and my age is {age}'

?

print(f_string)

print(F'My Name is {name} and my age is {age}')? # f and F are same

?

name = 'David'

age = 40

?

# f_string is already evaluated and won't change now

print(f_string)

?Output:

? 輸出:??

?

??

??

?

?My Name is Pankaj and my age is 34

My Name is Pankaj and my age is 34

My Name is Pankaj and my age is 34

?Python executes statements one by one and once f-string expressions are evaluated, they don’t change even if the expression value changes. That’s why in the above code snippets, f_string value remains same even after ‘name’ and ‘age’ variable has changed in the latter part of the program.

? Python會一一執行語句,并且一旦評估了f字符串表達式,即使表達式值更改,它們也不會更改。 這就是為什么在上面的代碼段中,即使在程序的后半部分更改了“ name”和“ age”變量后,f_string值仍保持不變。??

? 1.帶表達式和轉換的f字符串 (1. f-strings with expressions and conversions)?

?We can use f-strings to convert datetime to a specific format. We can also run mathematical expressions in f-strings.

? 我們可以使用f字符串將日期時間轉換為特定格式。 我們還可以在f字符串中運行數學表達式。??

?from datetime import datetime

?

name = 'David'

age = 40

d = datetime.now()

?

print(f'Age after five years will be {age+5}')? # age = 40

print(f'Name with quotes = {name!r}')? # name = David

print(f'Default Formatted Date = {d}')

print(f'Custom Formatted Date = {d:%m/%d/%Y}')

?Output:

? 輸出:??

?Age after five years will be 45

Name with quotes = 'David'

Default Formatted Date = 2018-10-10 11:47:12.818831

Custom Formatted Date = 10/10/2018

? 2. f字符串支持原始字符串 (2. f-strings support raw strings)?

?We can create raw strings using f-strings too.

? 我們也可以使用f字符串創建原始字符串。??

?print(f'Default Formatted Date:\n{d}')

print(fr'Default Formatted Date:\n {d}')

?Output:

? 輸出:??

?

??

??

?

?Default Formatted Date:

2018-10-10 11:47:12.818831

Default Formatted Date:\n 2018-10-10 11:47:12.818831

? 3.具有對象和屬性的f字符串 (3. f-strings with objects and attributes)?

?We can access object attributes too in f-strings.

? 我們也可以在f字符串中訪問對象屬性。??

?class Employee:

? ? id = 0

? ? name = ''

?

? ? def __init__(self, i, n):

? ? ? ? self.id = i

? ? ? ? self.name = n

?

? ? def __str__(self):

? ? ? ? return f'E[id={self.id}, name={self.name}]'

?

?

emp = Employee(10, 'Pankaj')

print(emp)

?

print(f'Employee: {emp}\nName is {emp.name} and id is {emp.id}')

?Output:

? 輸出:??

?E[id=10, name=Pankaj]

Employee: E[id=10, name=Pankaj]

Name is Pankaj and id is 10

? 4. f字符串調用函數 (4. f-strings calling functions)?

?We can call functions in f-strings formatting too.

? 我們也可以用f字符串格式調用函數。??

?def add(x, y):

? ? return x + y

?

?

print(f'Sum(10,20) = {add(10, 20)}')

?Output: Sum(10,20) = 30

? 輸出: Sum(10,20) = 30??

? 5.帶空格的f字符串 (5. f-string with whitespaces)?

?If there are leading or trailing whitespaces in the expression, they are ignored. If the literal string part contains whitespaces then they are preserved.

? 如果表達式中存在前導或尾隨空格,則將其忽略。 如果文字字符串部分包含空格,則將保留它們。??

?>>> age = 4 * 20

>>> f'? ?Age = {? age? ?}? '

'? ?Age = 80? '

? 6.帶f字符串的Lambda表達式 (6. Lambda expressions with f-strings)?

?We can use lambda expressions insidef-string expressions too.

? 我們也可以在字符串表達式內部使用lambda表達式 。??

?x = -20.45

print(f'Lambda Example: {(lambda x: abs(x)) (x)}')

?

print(f'Lambda Square Example: {(lambda x: pow(x, 2)) (5)}')

?Output:

? 輸出:??

?Lambda Example: 20.45

Lambda Square Example: 25

? 7. f弦的其他示例 (7. f-strings miscellaneous examples)?

?Let’s look at some miscellaneous examples of Python f-strings.

? 讓我們看一些Python f字符串的其他示例。??

?print(f'{"quoted string"}')

print(f'{{ {4*10} }}')

print(f'{{{4*10}}}')

?Output:

? 輸出:??

?quoted string

{ 40 }

{40}

?That’s all for python formatted strings aka f-strings.

? 這就是python格式的字符串,又稱f字符串。??

?

? GitHub Repository.

? GitHub存儲庫中檢出完整的python腳本和更多Python示例。?

?

?Reference: PEP-498, Official Documentation

? 參考: PEP-498 , 官方文檔??

?

? 翻譯自: https://www.journaldev.com/23592/python-f-strings-literal-string-interpolation

?

?字符串太長 pep8

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540198.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540198.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540198.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

備忘(持續更新。。。)

1、在springmvc這個框架里面,創建新的業務邏輯層,dao、service層至少需要一個接口,項目才能跑起來 2、獲取當前用戶桌面路徑 File desktopDir FileSystemView.getFileSystemView() .getHomeDirectory();String desktopPath desktopDir.getA…

[轉載] 字符串操作截取后面的字符串_對字符串的5個必知的熊貓操作

參考鏈接: 修剪Java中的字符串(刪除前導和尾隨空格) 字符串操作截取后面的字符串 We have to represent every bit of data in numerical values to be processed and analyzed by machine learning and deep learning models. However, strings do not usually co…

更改域控制器的計算機名

林功能級別必須為Windows Server 2003及以上 1. netdom computername Server08-1.contoso.com /add:08Server1.contoso.com 2. netdom computername Server08-1.contoso.com /makeprimary:08Server1.contoso.com 3. Restart your computer 4. netdom computername 08Server1.co…

[轉載] Google Java代碼規范

參考鏈接: 使用Java計算文本文件txt中的行數/單詞數/字符數和段落數 原文地址:https://google.github.io/styleguide/javaguide.html GIthub上GoogleCode風格的配置文件(支持Eclipse IDE和IntelliJ IDE):https://git…

SQL PASS西雅圖之行——簽證篇

本人有幸通過IT168&itpub的站慶活動http://www.itpub.net/thread-1716961-1-1.html,并應微軟邀請參加了在西雅圖舉辦的The Conference for SQL Server Professionals(簡稱SQL-PASS)。 SQL-PASS會議計劃于2012年11月6日-9日舉行&#xff0…

[轉載] java8 lambda表達式 List轉為Map

參考鏈接&#xff1a; 使用Lambda表達式檢查字符串在Java中是否僅包含字母 public static void main(String[] args) { List<User> userList new ArrayList<User>(); User user0 new User("han1", "男1", 20); User user1 new User("…

11.python并發入門(part5 event對象)

一、引入event。 每個線程&#xff0c;都是一個獨立運行的個體&#xff0c;并且每個線程的運行狀態是無法預測的。 如果一個程序中有很多個線程&#xff0c;程序的其他線程需要判斷某個線程的運行狀態&#xff0c;來確定自己下一步要執行哪些操作。 threading模塊中的event對象…

[轉載] Java 將字符串首字母轉為大寫 - 利用ASCII碼偏移

參考鏈接&#xff1a; 使用ASCII值檢查Java中的字符串是否僅包含字母 將字符串name 轉化為首字母大寫。普遍的做法是用subString()取第一個字母轉成大寫再與之后的拼接&#xff1a; str str.substring(0, 1).toUpperCase() str.substring(1); 看到一種效率更高的做法&…

俞永福卸任阿里大文娛董事長,改任 eWTP 投資組長

兩天前&#xff08;11月13日&#xff09;&#xff0c;阿里文娛董事長俞永福離職的消息&#xff0c;在互聯網圈炸了鍋。但很快&#xff0c;俞本人就在微博做了澄清&#xff0c;并稱“永遠幸福&#xff0c;我不會離開”。然而就在今天&#xff08;11月15日&#xff09;&#xff0…

[轉載] java提取字符串中的字母數字

參考鏈接&#xff1a; 使用Regex檢查字符串在Java中是否僅包含字母 String str "adsf adS DFASFSADF阿德斯防守對方asdfsadf37《&#xff1f;&#xff1a;&#xff1f;%#&#xffe5;%#&#xffe5;%#$%#$%^><?1234"; str str.replaceAll("[^a-zA-…

snort的詳細配置

前一段一直在做snort入侵檢測系統的安裝以及配置&#xff0c;看了很多的網上資料&#xff0c;也算是總結了下前輩的經驗吧。需要的軟件包&#xff1a;1、httpd-2.2.6.tar.gz2、mysql-5.1.22-rc-linux-i686-icc-glibc23.tar.gz3、php-5.2.4.tar.bz24、acid-0.9.6b23.tar.gz5、ad…

[轉載] Java:獲取數組中的子數組的多種方法

參考鏈接&#xff1a; Java中的數組Array 我的個人博客&#xff1a;zhang0peter的個人博客 Java&#xff1a;從一個數組中創建子數組 使用Arrays.copyOfRange函數 Arrays.copyOfRange支持&#xff1a;boolean[]&#xff0c; byte[] &#xff0c;char[]&#xff0c;double…

[轉載] Java中Array(數組)轉List(集合類)的幾種方法

參考鏈接&#xff1a; Java中的數組類Array 1、循環。新建List類&#xff0c;循環填充。 2、利用Arrays類的靜態方法asList()。 Arrays.asList(T[])返回Arrays類的一個內部內List(T)&#xff0c;此類繼承自AbstractList&#xff0c;不可增刪。若想要一個可以增刪的List類&am…

Linux查看系統cpu個數、核心書、線程數

Linux查看系統cpu個數、核心書、線程數 現在cpu核心數、線程數越來越高&#xff0c;本文將帶你了解如何確定一臺服務器有多少個cpu、每個cpu有幾個核心、每個核心有幾個線程。 查看物理cpu個數 cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l 查看核…

[轉載] java中數組的反射的探究

參考鏈接&#xff1a; Java中的反射數組類reflect.Array 數組的反射有什么用呢&#xff1f;何時需要使用數組的反射呢&#xff1f;先來看下下面的代碼&#xff1a; Integer[] nums {1, 2, 3, 4}; Object[] objs nums; //這里能自動的將Integer[]轉成Object[] Object obj n…

防火墻iptables之常用腳本

防火墻iptables之常用腳本 轉自&#xff1a;http://zhujiangtao.blog.51cto.com/6387416/1286490 標簽&#xff1a;防火墻 主機 1。不允許別人ping我的主機&#xff0c;但是我可以ping別人的主機 #!/bin/bash iptables -F iptables -X iptables -Z modprobe ip_tables modprobe…

[轉載] java中50個關鍵字以及各自用法大全

參考鏈接&#xff1a; Java中的默認數組值 關鍵字和保留字的區別 正確識別java語言的關鍵字&#xff08;keyword&#xff09;和保留字&#xff08;reserved word&#xff09;是十分重要的。Java的關鍵字對java的編譯器有特殊的意義&#xff0c;他們用來表示一種數據類型&…

NFS 共享存儲

服務器客戶端yum -y install rpcbind nfs-utils 服務器 vim /etc/exports /data 192.168.10.0/24(rw,sync,no_root_squash) * ro # 只讀權限 * rw # 讀寫權限 * sync # 同步&#xff0c;數據更安全&#xff0c;速度慢 * async #異步&#xff0c;速度快&#xff0c;效率高&a…

[轉載] Java中的final變量、final方法和final類

參考鏈接&#xff1a; Java中的final數組 &#xff5c; Final arrays 1、final變量 final關鍵字可用于變量聲明&#xff0c;一旦該變量被設定&#xff0c;就不可以再改變該變量的值。通常&#xff0c;由final定義的變量為常量。例如&#xff0c;在類中定義PI值&#xff0c;可…

Linux基礎篇_01_計算機概論

學習資料&#xff1a;《鳥哥的Linux私房菜&#xff08;基礎篇&#xff09;》部分&#xff1a;Linux的規劃與安裝 時間&#xff1a;20130225 學習筆記&#xff1a;計算機定義&#xff1a;接受使用者輸入指令與數據&#xff0c; 經由中央處理器的數學與邏輯單元運算處理后&#x…