Python programma, lai pārbaudītu, vai virkne ir tukša vai nav
Python virknes ir nemainīgas, un tām ir sarežģītāka apstrāde, apspriežot to darbības. Ņemiet vērā, ka virkne ar atstarpēm patiesībā ir tukša virkne, bet tās izmērs nav nulle. Šajā rakstā tika apspriesta arī šī problēma un tās risinājums. Apskatīsim dažādas metodes Pārbaudiet, vai virkne ir tukša Python .
Piemērs
Input: [' '] Output: Yes Explanation: In this, We are checking if the string is empty or not.
Pārbaudiet tukšo virkni Python
Šeit ir dažādas metodes, kā pārbaudīt, vai virkne Python ir tukša.
- Izmantojot len()
- Izmantojot not ()
- Izmantojot not + str.strip()
- Izmantojot not + str.isspace
- Izmantojot saraksta izpratni
- Izmantojot Bool
- Izmantojot sloksnes metodes
- Izmantojot un Operator + strip() funkcija
- Funkcijas all() izmantošana
- Izmantojot izmēģināt/izņemot
Python pārbaudes virkne tukša, izmantojot Len()
Izmantojot tikai() ir visvispārīgākā metode nulles garuma virkņu pārbaudei. Pat ja tajā tiek ignorēts fakts, ka virkne ar tikai atstarpēm arī ir praktiski jāuzskata par tukšu virkni, pat ja tā nav nulle.
Python3
# initializing string> test_str1> => ''> test_str2> => ' '> # checking if string is empty> print> (> 'The zero length string without spaces is empty ? : '> , end> => '')> if> (> len> (test_str1)> => => 0> ):> > print> (> 'Yes'> )> else> :> > print> (> 'No'> )> # prints No> print> (> 'The zero length string with just spaces is empty ? : '> , end> => '')> if> (> len> (test_str2)> => => 0> ):> > print> (> 'Yes'> )> else> :> > print> (> 'No'> )> |
Izvade
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No
Python pārbaudes virkne ir tukša izmantojot Not ()
Operators not var arī veikt uzdevumu, kas ir līdzīgs len() un pārbauda 0 garuma virkni, taču tāpat kā iepriekš, tā uzskata, ka virkne ar tikai atstarpēm arī nav tukša, kam praktiski nevajadzētu būt patiesībai.
Python3
# initializing string> test_str1> => ''> test_str2> => ' '> # checking if string is empty> print> (> 'The zero length string without spaces is empty ? : '> , end> => '')> if> (> not> test_str1):> > print> (> 'Yes'> )> else> :> > print> (> 'No'> )> # prints No> print> (> 'The zero length string with just spaces is empty ? : '> , end> => '')> if> (> not> test_str2):> > print> (> 'Yes'> )> else> :> > print> (> 'No'> )> |
Izvade
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No
Python tukša virkne iekšā nedziedāt + str.strip()
Tukšas + nulles garuma virknes problēmu, iespējams, var novērst, izmantojot strip(), strip() atgriež true, ja tā saskaras ar atstarpēm, tāpēc tās pārbaude var atrisināt problēmu, kas saistīta ar tīri tukšas virknes pārbaudi.
Python3
# initializing string> test_str1> => ''> test_str2> => ' '> # checking if string is empty> print> (> 'The zero length string without spaces is empty ? : '> , end> => '')> if> (> not> (test_str1> and> test_str1.strip())):> > print> (> 'Yes'> )> else> :> > print> (> 'No'> )> # prints Yes> print> (> 'The zero length string with just spaces is empty ? : '> , end> => '')> if> (> not> (test_str2> and> test_str2.strip())):> > print> (> 'Yes'> )> else> :> > print> (> 'No'> )> |
Izvade
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes
Pārbaudiet Empty String Python u nedziedāt + str.isspace
Darbojas līdzīgi kā iepriekš minētā metode un pārbauda, vai virknē nav atstarpes. Šī metode ir efektīvāka, jo strip() prasa veikt arī sloksnes darbību, kas ņem skaitļošanas slodzes, ja nē. vietu skaits ir labs.
Python3
# initializing string> test_str1> => ''> test_str2> => ' '> # checking if string is empty> print> (> 'The zero length string without spaces is empty ? : '> , end> => '')> if> (> not> (test_str1> and> not> test_str1.isspace())):> > print> (> 'Yes'> )> else> :> > print> (> 'No'> )> # prints Yes> print> (> 'The zero length string with just spaces is empty ? : '> , end> => '')> if> (> not> (test_str2> and> not> test_str2.isspace())):> > print> (> 'Yes'> )> else> :> > print> (> 'No'> )> |
Izvade
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes
Pārbaudiet, vai virkne ir tukša vai neizmanto saraksta izpratni
Šī pieeja ietver teksta parsēšanu rakstzīmju sarakstā, izmantojot saraksta izpratni, un pēc tam nosaka, vai saraksts ir tukšs. Mēs varam novērtēt, vai virkne ir tukša, novērtējot saraksta patiesumu.
Python3
string> => ''> x> => [> 'no'> if> len> (string)>>> ]> print> (x)> |
Izvade
['yes']
Pārbaudiet Python Empty String vai Not using Bool
Viena pieeja ir izmantot bool funkcija . Funkcija Bool atgriež False tukšām virknēm un True, ja virknes nav tukšas. Šeit ir piemērs, kā izmantot bool funkciju, lai pārbaudītu, vai virkne ir tukša.
Python3
# Initializing a string> test_str> => ''> # Checking if the string is empty> if> not> bool> (test_str):> > print> (> 'The string is empty.'> )> else> :> > print> (> 'The string is not empty.'> )> #This code is contributed by Edula Vinay Kumar Reddy> |
Izvade
The string is empty.
Varat arī izmantot bool funkciju, lai pārbaudītu, vai virkne ir tukša, pēc tam, kad ir noņemtas sākuma vai beigu atstarpes, izmantojot svītru metodi:
Python3
# Initializing a string> test_str> => ' '> # Checking if the string is empty after removing leading and trailing whitespaces> if> not> bool> (test_str.strip()):> > print> (> 'The string is empty.'> )> else> :> > print> (> 'The string is not empty.'> )> #This code is contributed by Edula Vinay Kumar Reddy> |
Izvade
The string is empty.
Python Pārbaudiet, vai virkne ir tukšs, izmantojot sloksnes metodi
Šeit mēs izmantosim Python strip() metodes lai pārbaudītu, vai virkne ir tukša vai nav.
Python3
#input empty with and without spaces string> s> => ''> str> => ' '> > if> s.strip():> > print> (f> 'string, string1 = '{s}', with no spaces is not empty'> )> else> :> > print> (f> 'string, string1 = '{s}', with no spaces is empty'> )> > if> str> .strip():> > print> (f> 'string, string2 = '{str}', with spaces is not empty'> )> else> :> > print> (f> 'string, string2 = '{str}', with spaces is empty'> )> |
Izvade
string, string1 = '', with no spaces is empty string, string2 = ' ', with spaces is empty
Pārbaudiet, vai virkne ir tukša vai netiek lietota, un Operator + strip() funkcija
Šajā pieejā operators un tiek izmantots, lai apvienotu divus testus: lai noteiktu, vai virkne nav None, un noteiktu, vai virknes svītrotā versija ir tukša. Sākošās un beigu atstarpes rakstzīmes no virknes izslēdz funkcija strip().
Python3
#input empty with and without spaces string> string1> => ''> string2> => ' '> > if> string1> and> string1.strip():> > print> (f> 'string, string1 = '{string1}', with no spaces is not empty'> )> else> :> > print> (f> 'string, string1 = '{string1}', with no spaces is empty'> )> > if> string2> and> string2.strip():> > print> (f> 'string, string2 = '{string2}', with spaces is not empty'> )> else> :> > print> (f> 'string, string2 = '{string2}', with spaces is empty'> )> |
Izvade
string, string1 = '', with no spaces is empty string, string2 = ' ', with spaces is empty
Python Pārbaudiet, vai virkne ir tukšs Izmantojot all() funkciju
Funkcijas all() atgriešanas vērtībai kā ievade ir nepieciešama Iterable. Ja iterable ir tukšs vai visi tā dalībnieki ir patiesi, vērtība ir patiesa. Funkcija all() var noteikt, vai virkne ir tukša vai visas tās rakstzīmes ir nepatiesas (tukša virkne), saņemot virkni kā atkārtojamu rakstzīmju skaitu.
Python3
string> => ''> if> all> (char.isspace()> for> char> in> string):> > print> (> 'The string is empty'> )> else> :> > print> (> 'The string is not empty'> )> |
Izvade
The string is empty
Bool pieejai, lai pārbaudītu, vai virkne ir tukša vai nav, ir a laika sarežģītība O(1), jo tā vienkārši pārbauda virknes patiesības vērtību, kas ir nemainīga laika darbība. The Palīgtelpa i s arī O(1), jo tam ir nepieciešams tikai viens Būla mainīgais, lai saglabātu virknes patiesības vērtību.
Python pārbaudiet tukšu virkni, izmantojot Mēģiniet/izņemot
Izmantojot try-izņemot bloku, programmā Python varat noteikt, vai virkne ir tukša. Varat noķert un novērst konkrētus izņēmumus, kas var rasties, koda izpildes laikā, izmantojot bloku try-except. Varat graciozi pārvaldīt apstākļus, kad paredzat iespējamu kļūdu, piemēram, pārbaudot, vai virkne nav tukša, izmantojot bloku try-izņemot.
Python3
# Initialize an empty string> string> => ''> try> :> > # Try to access the first character of the string> > string[> 0> ]> > # If no exception is raised, print 'The string is not empty.'> > print> (> 'The string is not empty.'> )> except> :> > # If a ValueError exception is raised, print 'The string is empty.'> > print> (> 'The string is empty.'> )> |
Izvade
The string is empty
Sarežģītības analīze:
Šim kodam ir nemainīga laika sarežģītība O(1), jo tas mēģina piekļūt tikai virknes pirmajai rakstzīmei, kas aizņem tikpat daudz laika neatkarīgi no virknes garuma.