Tools for working with strings


music21.common.stringTools.camelCaseToHyphen(usrStr: str, replacement: str = '-') → str

Given a camel-cased string, or a mixture of numbers and characters, create a space separated string.

The replacement can be specified to be something besides a hyphen, but only a single character and not (for internal reasons) an uppercase character.

code from

>>> common.camelCaseToHyphen('movementName')

First letter can be uppercase as well:

>>> common.camelCaseToHyphen('MovementName')
>>> common.camelCaseToHyphen('movementNameName')
>>> common.camelCaseToHyphen('fileName', replacement='_')

Some things you cannot do:

>>> common.camelCaseToHyphen('fileName', replacement='NotFound')
Traceback (most recent call last):
ValueError: Replacement must be a single character.
>>> common.camelCaseToHyphen('fileName', replacement='A')
Traceback (most recent call last):
ValueError: Replacement cannot be an uppercase character.
music21.common.stringTools.formatStr(msg, *arguments, **keywords) → str

Format one or more data elements into string suitable for printing straight to stderr or other outputs

>>> a = common.formatStr('test', '1', 2, 3)
>>> print(a)
test 1 2 3
music21.common.stringTools.getMd5(value=None) → str

Return an md5 hash from a string. If no value is given then the current time plus a random number is encoded.

>>> common.getMd5('test')
music21.common.stringTools.getNumFromStr(usrStr: str, numbers: str = '0123456789') → Tuple[str, str]

Given a string, extract any numbers. Return two strings, the numbers (as strings) and the remaining characters.

>>> common.getNumFromStr('23a')
('23', 'a')
>>> common.getNumFromStr('23a954Hello')
('23954', 'aHello')
>>> common.getNumFromStr('')
('', '')
music21.common.stringTools.hyphenToCamelCase(usrStr: str, replacement: str = '-') → str

Given a hyphen-connected-string, change it to a camelCaseConnectedString.

The replacement can be specified to be something besides a hyphen.

>>> common.hyphenToCamelCase('movement-name')
>>> common.hyphenToCamelCase('movement_name', replacement='_')

Safe to call on a string lacking the replacement character:

>>> common.hyphenToCamelCase('voice')

And on “words” beginning with numbers:

>>> common.hyphenToCamelCase('music-21')
music21.common.stringTools.normalizeFilename(name: str) → str

take a name that might contain unicode characters, punctuation, or spaces and normalize it so that it is POSIX compliant (except for the limit on length).

Takes in a string or unicode string and returns a string (unicode in Py3) without any accented characters.

>>> common.normalizeFilename('03-Niccolò all’lessandra.not really.xml')
music21.common.stringTools.removePunctuation(s: str) → str

Remove all punctuation from a string.

>>> common.removePunctuation('This, is! my (face).')
'This is my face'
music21.common.stringTools.spaceCamelCase(usrStr: str, replaceUnderscore=True, fixMeList=None) → str

Given a camel-cased string, or a mixture of numbers and characters, create a space separated string.

If replaceUnderscore is True (default) then underscores also become spaces (but without the _)

>>> common.spaceCamelCase('thisIsATest')
'this Is A Test'
>>> common.spaceCamelCase('ThisIsATest')
'This Is A Test'
>>> common.spaceCamelCase('movement3')
'movement 3'
>>> common.spaceCamelCase('opus41no1')
'opus 41 no 1'
>>> common.spaceCamelCase('opus23402no219235')
'opus 23402 no 219235'
>>> common.spaceCamelCase('opus23402no219235').title()
'Opus 23402 No 219235'

There is a small list called fixMeList that can fix mistakes.

>>> common.spaceCamelCase('PMFC22')
'PMFC 22'
>>> common.spaceCamelCase('hello_myke')
'hello myke'
>>> common.spaceCamelCase('hello_myke', replaceUnderscore=False)
music21.common.stringTools.stripAccents(inputString: str) → str

removes accents from unicode strings.

>>> s = 'trés vite'
>>> 'é' in s
>>> common.stripAccents(s)
'tres vite'
music21.common.stringTools.whitespaceEqual(a: str, b: str) → bool

returns True if a and b are equal except for whitespace differences

>>> a = "    hello \n there "
>>> b = "hello there"
>>> c = " bye there "
>>> common.whitespaceEqual(a, b)
>>> common.whitespaceEqual(a, c)