How to capitalize specific letters in a string given certain rules

how can i convert each alternate character of a string to uppercase in python
java program change a string such that first character is upper case second is lower case and so on
convert string to uppercase in c
c program to convert lowercase character to uppercase character
convert all lowercase letters to uppercase letters and vice versa. in python
how to convert lowercase to uppercase in java using ascii
convert first character of string to uppercase in java
convert lowercase character to uppercase in java

I am massaging strings so that the 1st letter of the string and the first letter following either a dash or a slash needs to be capitalized.

So the following string:

test/string - this is a test string

Should look look like so:

Test/String - This is a test string

So in trying to solve this problem my 1st idea seems like a bad idea - iterate the string and check every character and using indexing etc. determine if a character follows a dash or slash, if it does set it to upper and write out to my new string.

def correct_sentence_case(test_phrase):

corrected_test_phrase = ''

firstLetter = True

for char in test_phrase:

    if firstLetter:

        corrected_test_phrase += char.upper()

        firstLetter = False

    #elif char == '/':

        corrected_test_phrase += char

This just seems VERY un-pythonic. What is a pythonic way to handle this?

Something along the lines of the following would be awesome but I can't pass in both a dash and a slash to the split:

corrected_test_phrase = ' - '.join(i.capitalize() for i in test_phrase.split(' - '))

Which I got from this SO:

Convert UPPERCASE string to sentence case in Python

Any help will be appreciated :)

I was able to accomplish the desired transformation with a regular expression:

import re
capitalized = re.sub(
    '(^|[-/])\s*([A-Za-z])', lambda match: match[0].upper(), phrase)

The expression says "anywhere you match either the start of the string, ^, or a dash or slash followed by maybe some space and a word character, replace the word character with its uppercase."


How to capitalize the first character of each word in a string, private String capitalize(final String line) { return Character. getDefaultDelimiters(){ // If no delimiter specified, "Capitalize after space" rule is set by default. If no specific individual is referred to, do not capitalize titles of even very high ranking government officials. EXAMPLE . Capitalize important words in compound titles used with names, but do not capitalize prefixes or suffixes added to the titles. EXAMPLES . 3. major words in titles of books, articles, and songs . EXAMPLE

If you don't want to go with a messy splitting-joining logic, go with a regex:

import re

string = 'test/string - this is a test string'

             lambda match:, string))
# Test/String - This is a test string

Convert characters of a string to opposite case, Given a string, convert the characters of the string into opposite case,i.e. if a character is lower case than convert it into upper case and vice-versa. Examples: 4 How to capitalize specific letters in a string given certain rules Dec 27 '18 4 Handling nested JSON in Python Apr 24 '18 4 Partitioning a string and splitting it every n characters Apr 10 '18

Using double split

import re
' - '.join([i.strip().capitalize() for i in re.split(' - ','/'.join([i.capitalize() for i in re.split('/',test_phrase)]))])

Ciarcia's Circuit Cellar, RIGHT CONTEXT TERMINATOR IN THE INPUT STRING Since all rules in a single for a given letter contains a parenrepresents any nonalphabetic quality words or it and letter rules in character - specific matched ) , and the the symbols to the to prepare for certain the entire input string to uppercase for intonation and  2 How to capitalize specific letters in a string given certain rules Dec 27 '18 1 Pass path names with spaces to command line Oct 29 '18 1 Display field other than __str__ Feb 1 '17

7.1. string — Common string operations, format() built-in. The method is provided so that subclasses can override it. This allows the formatting of a value to be dynamically specified. See the Outputs the number in base 16, using upper- case letters for the digits above 9. 'n'​, Number. The string module provides a Template class that implements these rules. I'm having a problem in my program that will replace each character in the string entered (specifically from args[x]). I am able to get the first character in the string but what I don't know how String Methods, capitalize (): Return a copy of the string with only its first character capitalized. Default encoding is the current default string encoding. errors may be given to set a Return true if the string ends with the specified suffix , otherwise return false. The Excel REPLACE and SUBSTITUTE functions are very similar to each other in that both are designed to swap text strings. The differences between the two functions are as follows: SUBSTITUTE replaces one or more instances of a given character or a text string. So, if you know the text to be replaced, use the Excel SUBSTITUTE function.

String.ToLower Method (System), The following example converts two strings of uppercase characters to The casing rules of the culture specified by the culture parameter determine the way the  If a string has vowels, return an empty string if the string is a single character. For strings of any other length, cut the vowels out systematically. Return the altered string.

  • Why doesn't your "wanted way" work? Split, capitalize and join
  • @SimonF Because it doesn't produce Test/String - This is a test string but Test/string - This is a test string
  • @SimonF - It works super nicely but I can't pass both dashes and slashes to the split - I probably should have said that in my question - it is unclear - ty
  • Explicit is better than implicit. Simple is better than complex. Sparse is better than dense. Readability counts.; do it in more than one step.
  • You have two different criteria for splitting. The one delimiter is " - " and the other is "\". Do your split/capitalize in 2 passes. Split first on " - " into a list and then split every element of that list on "\".
  • 7 seconds ahead of me, nice. Just be wary with using \w as it matches any of [a-zA-Z0-9_] (In a second thought it doesn't really matter in this particular case)
  • Haha I noticed that. Very astute of you, I'll make the tweak
  • Wow - just tested and it works. Why do I always make stuff more complicated than it needs to be - ty :)