Popular recipes tagged "string"http://code.activestate.com/recipes/tags/string/2015-01-24T21:21:27-08:00ActiveState Code RecipesBytes-to-human / human-to-bytes converter (Python)
2012-02-02T16:09:52-08:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578019-bytes-to-human-human-to-bytes-converter/
<p style="color: grey">
Python
recipe 578019
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/bytes/">bytes</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/human/">human</a>, <a href="/recipes/tags/humanize/">humanize</a>, <a href="/recipes/tags/string/">string</a>).
Revision 15.
</p>
<p>Here goes.</p>
Use PrettyTable and xtopdf to create PDF tables with borders, alignment and padding (Python)
2015-01-24T21:21:27-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579012-use-prettytable-and-xtopdf-to-create-pdf-tables-wi/
<p style="color: grey">
Python
recipe 579012
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/prettytable/">prettytable</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/tabular/">tabular</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>).
</p>
<p>This recipe shows how to create tabular data in PDF format, supporting neat borders, and alignment and padding of columns, using the Python libraries called PrettyTable and xtopdf.</p>
Random Password Generation (Python)
2014-08-10T15:50:40-07:00Paul Wolfhttp://code.activestate.com/recipes/users/4190553/http://code.activestate.com/recipes/578920-random-password-generation/
<p style="color: grey">
Python
recipe 578920
by <a href="/recipes/users/4190553/">Paul Wolf</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/strong/">strong</a>).
</p>
<p>Generate a password (or other secure token) using a pattern language similar to regular expressions. We'll use the <code>strgen</code> module that enables a user to generate test data, unique ids, passwords, vouchers or other randomized data very quickly using a template language. The template language is superficially similar to regular expressions but instead of defining how to match or capture strings, it defines how to generate randomized strings.</p>
String length (Batch)
2013-10-10T16:27:48-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578664-string-length/
<p style="color: grey">
Batch
recipe 578664
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/length/">length</a>, <a href="/recipes/tags/string/">string</a>).
Revision 2.
</p>
<p>A way for checking string length.</p>
Formating strings (print a table) (Batch)
2013-06-18T07:52:03-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578562-formating-strings-print-a-table/
<p style="color: grey">
Batch
recipe 578562
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/table/">table</a>).
</p>
<p>As you know there are no escape characters such as "\t" in the windows command language but it does not mean that we can not format text. Command prompt has its own tricks. At firstly, you need declare enabledelayedexpansion after setlocal command in your batch file to get access for some interesting things; secondly, use <code><nul set /p "str=[string]"</code> construction which is equal print function in C language. OK, next batch file print multiplication table.</p>
Split string sample (Batch)
2013-08-13T11:29:44-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578638-split-string-sample/
<p style="color: grey">
Batch
recipe 578638
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/split/">split</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>Target: we have a task to split the string ("this is a string") on the characters. Possible solution:</p>
Reversing string (JavaScript)
2013-02-18T09:27:33-08:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578464-reversing-string/
<p style="color: grey">
JavaScript
recipe 578464
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/reverse/">reverse</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>Two ways reverse string.</p>
String to Binary (Python)
2014-07-30T07:20:22-07:00p@ntut$http://code.activestate.com/recipes/users/4183895/http://code.activestate.com/recipes/578291-string-to-binary/
<p style="color: grey">
Python
recipe 578291
by <a href="/recipes/users/4183895/">p@ntut$</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/to/">to</a>).
Revision 2.
</p>
<p>String to binary snippet, python3+</p>
Decimal Number To Byte(s) And String To Byte(s) Converter. (Python)
2012-01-24T21:11:47-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578025-decimal-number-to-bytes-and-string-to-bytes-conver/
<p style="color: grey">
Python
recipe 578025
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/bytes/">bytes</a>, <a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>A function to convert decimal integer numbers, (from 0 to 255), into byte(s) format.
Another function calling the above function to convert ASCII strings into byte(s) format.</p>
<p>Python 3.1.3 (r313:86834, Nov 28 2010, 10:01:07)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.</p>
<pre class="prettyprint"><code>>>> exec(open('/home/G0LCU/Desktop/Code/d2b.py').read())
>>> a=78
>>> type(a)
<class 'int'>
>>> b=d2b(a)
>>> print(b)
b'N'
>>> type(b)
<class 'bytes'>
>>> text="\x00(C)2012, B.Walker, G0LCU.\xFF"
>>> len(text)
27
>>> type(text)
<class 'str'>
>>> newtext=t2b(text)
>>> len(newtext)
27
>>> print(newtext)
b'\x00(C)2012, B.Walker, G0LCU.\xff'
>>> type(newtext)
<class 'bytes'>
</code></pre>
<p>It requires NOTHING special at all to work and can be run like above or imported from
the correct "Lib" drawer/folder/directorfy as:-</p>
<pre class="prettyprint"><code>>>> import d2b
</code></pre>
<p>And when imported called as:-</p>
<pre class="prettyprint"><code>>>> d2b.d2b(some_number, optional_some_other_mumber)<RETURN/ENTER>
</code></pre>
<p>OR</p>
<pre class="prettyprint"><code>>>> d2b.t2b(some_ASCII_string)<RETURN/ENTER>
</code></pre>
<p>Read the code for much more information...</p>
<p>Issued under the GPL2 licence.</p>
<p>Enjoy finding simple solutions to often very difficult problems.</p>
<p>Bazza, G0LCU.</p>
Easy ansi color plus more. (Ruby)
2014-04-16T15:36:04-07:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/578018-easy-ansi-color-plus-more/
<p style="color: grey">
Ruby
recipe 578018
by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a>
(<a href="/recipes/tags/ansi/">ansi</a>, <a href="/recipes/tags/clear/">clear</a>, <a href="/recipes/tags/console/">console</a>, <a href="/recipes/tags/cursor/">cursor</a>, <a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/movement/">movement</a>, <a href="/recipes/tags/output/">output</a>, <a href="/recipes/tags/ruby/">ruby</a>, <a href="/recipes/tags/screen/">screen</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/underline/">underline</a>).
</p>
<p>This quick class extends the base String class to add the ability to chain escape codes onto your output. For instance: puts 'String'.bold.underline.blink.red for something truly hideous. Aside from the colors (all are supported, but I haven't put in support for background colors as of the time of this post), cursor placement (ymmv based on the term type), screen clearing, bold, underline, blink reverse, conceal are all handled as well.</p>
lreplace() and rreplace(): Replace the beginning and ends of a strings (Python)
2010-06-04T02:18:48-07:00Dan McDougallhttp://code.activestate.com/recipes/users/4169722/http://code.activestate.com/recipes/577252-lreplace-and-rreplace-replace-the-beginning-and-en/
<p style="color: grey">
Python
recipe 577252
by <a href="/recipes/users/4169722/">Dan McDougall</a>
(<a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/replace/">replace</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>Python newbies will often make the following mistake (I certainly have =):</p>
<pre class="prettyprint"><code>>>> test = """this is a test:
... tis the season for mistakes."""
>>> for line in test.split('\n'):
... print line.lstrip('this')
...
is a test
the season for mistakes.
</code></pre>
<p>The mistake is assuming that lstrip() (or rstrip()) strips a string (whole) when it actually strips all of the provided characters in the given string. Python actually comes with no function to strip a string from the left-hand or right-hand side of a string so I wrote this (very simple) recipe to solve that problem. Here's the usage:</p>
<pre class="prettyprint"><code>>>> test = """this is a test:
... tis the season for mistakes."""
>>> for line in test.split('\n'):
... print lreplace('this', '', line)
...
is a test
tis the season for mistakes.
</code></pre>
<p>I really wish Python had these functions built into the string object. I think it would be a useful addition to the standard library. It would also be nicer to type this:</p>
<pre class="prettyprint"><code>line.lreplace('this', '')
</code></pre>
<p>Instead of this:</p>
<pre class="prettyprint"><code>lreplace('this','',line)
</code></pre>
Implementation of Knuth–Morris–Pratt algorithm (C)
2011-10-15T00:33:41-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577908-implementation-of-knuthmorrispratt-algorithm/
<p style="color: grey">
C
recipe 577908
by <a href="/recipes/users/4168519/">Shao-chuan Wang</a>
(<a href="/recipes/tags/kmp/">kmp</a>, <a href="/recipes/tags/matching/">matching</a>, <a href="/recipes/tags/string/">string</a>).
Revision 2.
</p>
<p>Knuth–Morris–Pratt (KMP) is a linear time string matching algorithm.</p>
<p><a href="http://en.wikipedia.org/wiki/Knuth%25E2%2580%2593Morris%25E2%2580%2593Pratt_algorithm" rel="nofollow">http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm</a></p>
<p>This recipe gives a C-version implementation.</p>
get all possible combinations of characters given a string (Python)
2011-08-15T04:15:42-07:00Yanghttp://code.activestate.com/recipes/users/4178968/http://code.activestate.com/recipes/577842-get-all-possible-combinations-of-characters-given-/
<p style="color: grey">
Python
recipe 577842
by <a href="/recipes/users/4178968/">Yang</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/permutation/">permutation</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>This will give a result that is more than a permutation, but all possible combinations. An example is when input is 'abc', the output would be:
a,ab,abc,ac,acb,b,ba,bac,bc,bca,c,ca,cab,cb,cba</p>
Search sequences for sub-sequence (Python)
2011-08-19T05:17:00-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577850-search-sequences-for-sub-sequence/
<p style="color: grey">
Python
recipe 577850
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/searching/">searching</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/sublist/">sublist</a>, <a href="/recipes/tags/substring/">substring</a>).
</p>
<p>The list and tuple index() method and <code>in</code> operator test for element containment, unlike similar tests for strings, which checks for sub-strings:</p>
<pre class="prettyprint"><code>>>> "12" in "0123"
True
>>> [1, 2] in [0, 1, 2, 3]
False
</code></pre>
<p>These two functions, search and rsearch, act like str.find() except they operate on any arbitrary sequence such as lists:</p>
<pre class="prettyprint"><code>>>> search([1, "a", "b", 2, 3], ["b", 2])
2
</code></pre>
Split Strings w/ Multiple Separators (Python)
2011-03-20T20:33:13-07:00Kenneth Reitzhttp://code.activestate.com/recipes/users/4177394/http://code.activestate.com/recipes/577616-split-strings-w-multiple-separators/
<p style="color: grey">
Python
recipe 577616
by <a href="/recipes/users/4177394/">Kenneth Reitz</a>
(<a href="/recipes/tags/split/">split</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/string_parsing/">string_parsing</a>).
Revision 5.
</p>
<p>Splits strings with multiple separators instead of one (e.g. <code>str.split()</code>).</p>
"Comma float" to float (Python)
2010-02-11T22:19:41-08:00Matevz Leskohttp://code.activestate.com/recipes/users/4173032/http://code.activestate.com/recipes/577054-comma-float-to-float/
<p style="color: grey">
Python
recipe 577054
by <a href="/recipes/users/4173032/">Matevz Lesko</a>
(<a href="/recipes/tags/comma/">comma</a>, <a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/string/">string</a>).
Revision 2.
</p>
<p>Convert comma separated floating point number (12,3456) to float.</p>
converting numbers to their alphabetical style (Python)
2011-03-17T06:57:06-07:00amir naghavihttp://code.activestate.com/recipes/users/4177294/http://code.activestate.com/recipes/577607-converting-numbers-to-their-alphabetical-style/
<p style="color: grey">
Python
recipe 577607
by <a href="/recipes/users/4177294/">amir naghavi</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/string/">string</a>).
Revision 4.
</p>
<p>converts numbers to alphabetical numbers </p>
C# String.Replace with a char array (Java)
2010-12-24T09:58:35-08:00John Hurlimanhttp://code.activestate.com/recipes/users/4174599/http://code.activestate.com/recipes/577516-c-stringreplace-with-a-char-array/
<p style="color: grey">
Java
recipe 577516
by <a href="/recipes/users/4174599/">John Hurliman</a>
(<a href="/recipes/tags/csharp/">csharp</a>, <a href="/recipes/tags/replace/">replace</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>Takes a string and replaces all characters matching a given array with a given string</p>
Auto Named Decriptors (Python)
2010-10-19T22:55:16-07:00Aloys Baillethttp://code.activestate.com/recipes/users/4175355/http://code.activestate.com/recipes/577426-auto-named-decriptors/
<p style="color: grey">
Python
recipe 577426
by <a href="/recipes/users/4175355/">Aloys Baillet</a>
(<a href="/recipes/tags/automatically/">automatically</a>, <a href="/recipes/tags/constants/">constants</a>, <a href="/recipes/tags/descriptors/">descriptors</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/name/">name</a>, <a href="/recipes/tags/string/">string</a>).
Revision 3.
</p>
<p>Using named Descriptors? Tired of duplicating the name of the instance in a string? A small metaclass can solve this.</p>
Split a string on capitalized / uppercase char using Python (Python)
2009-12-11T23:16:36-08:00activestatehttp://code.activestate.com/recipes/users/4172588/http://code.activestate.com/recipes/576984-split-a-string-on-capitalized-uppercase-char-using/
<p style="color: grey">
Python
recipe 576984
by <a href="/recipes/users/4172588/">activestate</a>
(<a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/string_parsing/">string_parsing</a>, <a href="/recipes/tags/text_processing/">text_processing</a>).
Revision 6.
</p>
<p>By user <a href="http://code.activestate.com/recipes/users/2629617/" rel="nofollow">http://code.activestate.com/recipes/users/2629617/</a> in comment on <a href="http://code.activestate.com/recipes/440698/" rel="nofollow">http://code.activestate.com/recipes/440698/</a> but modified slightly.</p>
<p>Splits any string on upper case characters.</p>
<p>Ex.</p>
<pre class="prettyprint"><code>>>> print split_uppercase("thisIsIt and SoIsThis")
this Is It and So Is This
</code></pre>
<p>note the two spaces after 'and'</p>