python 字符串 變量_檢查變量是否為字符串的Python程序

python 字符串 變量

Python | 檢查變量是否為字符串 (Python | Check if a variable is a string)

To check whether a defined variable is a string type or not, we can use two functions which are Python library functions,

要檢查定義的變量是否為字符串類型,我們可以使用兩個函數,它們是Python庫函數,

  1. Using isinstance()

    使用isinstance()

  2. Using type()

    使用type()

1)使用isinstance()函數檢查變量是否為字符串 (1) Checking a variable is a string or not using isinstance() function)

isinstance() function accepts two parameters – 1) variable name (object) and 2) data type (class) and returns whether an object is an instance of a class or of a subclass thereof.

isinstance()函數接受兩個參數– 1)變量名( 對象 )和2)數據類型( 類 ),并返回對象是類的實例還是其子類的實例。

Syntax:

句法:

    isinstance(obj, class_or_tuple)

Example:

例:

# variables
a = 100   # an integer variable
b = 10.23 # a float variable
c = 'A'   # a character variable 
d = 'Hello'   # a string variable
e = "Hello"   # a string variable 
# checking types
if isinstance(a, str):
print("Variable \'a\' is a type of string.")
else:
print("Variable \'a\' is not a type of string.")
if isinstance(b, str):
print("Variable \'b\' is a type of string.")
else:
print("Variable \'b\' is not a type of string.")
if isinstance(c, str):
print("Variable \'c\' is a type of string.")
else:
print("Variable \'c\' is not a type of string.")
if isinstance(d, str):
print("Variable \'d\' is a type of string.")
else:
print("Variable \'d\' is not a type of string.")
if isinstance(e, str):
print("Variable \'e\' is a type of string.")
else:
print("Variable \'e\' is not a type of string.")

Output

輸出量

Variable 'a' is not a type of string.
Variable 'b' is not a type of string.
Variable 'c' is a type of string.
Variable 'd' is a type of string.
Variable 'e' is a type of string.

2)使用type()函數檢查變量是否為字符串 (2) Checking a variable is string using type() function)

type() function accepts one parameter (others are optional), and returns its type.

type()函數接受一個參數(其他參數是可選的),并返回其類型。

Syntax:

句法:

    type(object)

Example:

例:

# variables
a = 100   # an integer variable
b = 10.23 # a float variable
c = 'A'   # a character variable 
d = 'Hello'   # a string variable
e = "Hello"   # a string variable 
# checking types
if type(a) == str:
print("Variable \'a\' is a type of string.")
else:
print("Variable \'a\' is not a type of string.")
if type(b) == str:
print("Variable \'b\' is a type of string.")
else:
print("Variable \'b\' is not a type of string.")
if type(c) == str:
print("Variable \'c\' is a type of string.")
else:
print("Variable \'c\' is not a type of string.")
if type(d) == str:
print("Variable \'d\' is a type of string.")
else:
print("Variable \'d\' is not a type of string.")
if type(e) == str:
print("Variable \'e\' is a type of string.")
else:
print("Variable \'e\' is not a type of string.")

Output

輸出量

Variable 'a' is not a type of string.
Variable 'b' is not a type of string.
Variable 'c' is a type of string.
Variable 'd' is a type of string.
Variable 'e' is a type of string.

翻譯自: https://www.includehelp.com/python/check-whether-a-variable-is-a-string-or-not.aspx

python 字符串 變量

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

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

相關文章

mysql shell

mysql 查詢10分鐘以內的數據:select *from t_agent where int_last_login>CURRENT_TIMESTAMP - INTERVAL 10 MINUTE; mysql關聯多表進行update更新操作UPDATE TrackINNER JOIN MVON Track.trkidMV.mvidSET Track.is_showMV.is_showWHERE trkid<6等同于UPDATE Track,MVSET…

kaggle計算機視覺比賽技巧,9. 計算機視覺 - 9.12. 實戰Kaggle比賽:圖像分類(CIFAR-10) - 《動手學深度學習》 - 書棧網 · BookStack...

9.12. 實戰Kaggle比賽&#xff1a;圖像分類(CIFAR-10)到目前為止&#xff0c;我們一直在用Gluon的data包直接獲取NDArray格式的圖像數據集。然而&#xff0c;實際中的圖像數據集往往是以圖像文件的形式存在的。在本節中&#xff0c;我們將從原始的圖像文件開始&#xff0c;一步…

qthread中獲取當前優先級_Linux中強大的top命令

top命令算是最直觀、好用的查看服務器負載的命令了。它實時動態刷新顯示服務器狀態信息&#xff0c;且可以通過交互式命令自定義顯示內容&#xff0c;非常強大。在終端中輸入top&#xff0c;回車后會顯示如下內容&#xff1a;top - 21:48:39 up 8:57, 2 users, load average: 0…

JavaScript中帶示例的String repeat()方法

JavaScript | 字符串repeat()方法 (JavaScript | String repeat() Method) The String.repeat() method in JavaScript is used to generate a string by repeating the calling string n number of times. n can be any integer from o to any possible number in JavaScript.…

Python生成驗證碼

#!/usr/bin/env python #coding:utf8 import random #方法1&#xff1a; str_codezxcvbnmasdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP0123456789new_codefor i in range(4):   new_coderandom.choice(str_code)print new_code #方法2&#xff1a; new_code[]def str_code…

snmp 獲得硬件信息_計算機網絡基礎課程—簡單網絡管理協議(SNMP)

簡單網絡管理協議(Simple Network Management Protocol)?除了提供網絡層服務的協議和使用那些服務的應用程序&#xff0c;因特網還需要運行一些讓管理員進行設備管理、調試問題、控制路由、監測機器狀態的軟件。這種行為稱為網絡管理。??隨著網絡技術的飛速發展&#xff0c;…

僵尸毀滅工程 服務器已停止運行,《僵尸毀滅工程》steam is not enabled錯誤解決方法...

Steam 上面的 Project Zomboid 因為帶有 VAC 所以建服開服需要 Steam服務器認證&#xff0c;這也是出現 steam is not enabled 錯誤主要原因&#xff0c;也是無法和普通零售正版所建的服務器聯機的罪魁禍首。分兩種情況(下面 Project Zomboid 均簡稱PZ)&#xff1a;1、steam版P…

spring boot 1.4默認使用 hibernate validator

spring boot 1.4默認使用 hibernate validator 5.2.4 Final實現校驗功能。hibernate validator 5.2.4 Final是JSR 349 Bean Validation 1.1的具體實現。 How to disable Hibernate validation in a Spring Boot project As [M. Deinum] mentioned in a comment on my original …

python mpi開銷_GitHub - hustpython/MPIK-Means

并行計算的K-Means聚類算法實現一&#xff0c;實驗介紹聚類是擁有相同屬性的對象或記錄的集合&#xff0c;屬于無監督學習&#xff0c;K-Means聚類算法是其中較為簡單的聚類算法之一&#xff0c;具有易理解&#xff0c;運算深度塊的特點.1.1 實驗內容通過本次課程我們將使用C語…

服務器修改開機啟動項,啟動項設置_服務器開機啟動項

最近很多觀眾老爺在苦覓關于啟動項設置的解答&#xff0c;今天欽編為大家綜合5條解答來給大家解開疑惑&#xff01; 有98%玩家認為啟動項設置_服務器開機啟動項值得一讀&#xff01;啟動項設置1.如何在bios設置硬盤為第一啟動項詳細步驟根據BIOS分類的不同操作不同&#xff1a;…

字符串查找字符出現次數_查找字符串作為子序列出現的次數

字符串查找字符出現次數Description: 描述&#xff1a; Its a popular interview question based of dynamic programming which has been already featured in Accolite, Amazon. 這是一個流行的基于動態編程的面試問題&#xff0c;已經在亞馬遜的Accolite中得到了體現。 Pr…

Ubuntu 忘記密碼的處理方法

Ubuntu系統啟動時選擇recovery mode&#xff0c;也就是恢復模式。接著選擇Drop to root shell prompt ,也就是獲取root權限。輸入命令查看用戶名 cat /etc/shadow &#xff0c;$號前面的是用戶名輸入命令&#xff1a;passwd "用戶名" 回車就可以輸入新密碼了轉載于:…

服務器mdl文件轉換,Simulink Project 中 MDL 到 SLX 模型文件格式的轉換

打開彈體示例項目并將 MDL 文件另存為 SLX運行以下命令以創建并打開“sldemo_slproject_airframe”示例的工作副本。Simulink.ModelManagement.Project.projectDemo(airframe, svn);rebuild_s_functions(no_progress_dialog);Creating sandbox for project.Created example fil…

vue 修改div寬度_Vue 組件通信方式及其應用場景總結(1.5W字)

前言相信實際項目中用過vue的同學&#xff0c;一定對vue中父子組件之間的通信并不陌生&#xff0c;vue中采用良好的數據通訊方式&#xff0c;避免組件通信帶來的困擾。今天筆者和大家一起分享vue父子組件之間的通信方式&#xff0c;優缺點&#xff0c;及其實際工作中的應用場景…

Java System類identityHashCode()方法及示例

系統類identityHashCode()方法 (System class identityHashCode() method) identityHashCode() method is available in java.lang package. identityHashCode()方法在java.lang包中可用。 identityHashCode() method is used to return the hashcode of the given object – B…

Linux中SysRq的使用(魔術鍵)

轉&#xff1a;http://www.chinaunix.net/old_jh/4/902287.html 魔術鍵&#xff1a;Linux Magic System Request Key Hacks 當Linux 系統不能正常響應用戶請求時, 可以使用SysRq小工具控制Linux. 一 SysRq的啟用與關閉 要想啟用SysRq, 需要在配置內核時設置Magic SysRq key (CO…

鏈接服務器訪問接口返回了消息沒有活動事務,因為鏈接服務器 SQLEHR 的 OLE DB 訪問接口 SQLNCLI10 無法啟動分布式事務。...

查看一下MSDTC啟動是否正確1、運行 regedt32&#xff0c;瀏覽至 HKEY_LOCAL_MACHINE\Software\Microsoft\MSDTC。添加一個 DWORD 值 TurnOffRpcSecurity&#xff0c;值數據為 1。2、重啟MS DTC服務。3、打開“管理工具”的“組件服務”。a. 瀏覽至"啟動管理工具"。b.…

micropython 蜂鳴器_基于MicroPython的TPYBoard微信遠程可燃氣體報警器的設計與實現...

前言在我們平時的生活中&#xff0c;經常看到因氣體泄漏發生爆炸事故的新聞。房屋起火、人體中毒等此類的新聞報道層出不窮。這種情況下&#xff0c;人民就發明了可燃氣體報警器。當工業環境、日常生活環境(如使用天然氣的廚房)中可燃性氣體發生泄露&#xff0c;可燃氣體報警器…

Java PropertyPermission getActions()方法與示例

PropertyPermission類的getActions()方法 (PropertyPermission Class getActions() method) getActions() method is available in java.util package. getActions()方法在java.util包中可用。 getActions() method is used to get the list of current actions in the form of…

源碼安裝nginx以及平滑升級

源碼安裝nginx以及平滑升級作者&#xff1a;尹正杰版權聲明&#xff1a;原創作品&#xff0c;謝絕轉載&#xff01;否則將追究法律責任。歡迎加入&#xff1a;高級運維工程師之路 598432640這個博客不方便上傳軟件包&#xff0c;我給大家把軟件包放到百度云鏈接&#xff1a;htt…